Files
tailscale/types/geo/units.go
T
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

192 lines
4.9 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package geo
import (
"math"
"strconv"
"strings"
"unicode"
)
const (
Degree Degrees = 1
Radian Radians = 1
Turn Turns = 1
Meter Distance = 1
)
// Degrees represents a latitude or longitude, in decimal degrees.
type Degrees float64
// ParseDegrees parses s as decimal degrees.
func ParseDegrees(s string) (Degrees, error) {
s = strings.TrimSuffix(s, "°")
f, err := strconv.ParseFloat(s, 64)
return Degrees(f), err
}
// MustParseDegrees parses s as decimal degrees, but panics on error.
func MustParseDegrees(s string) Degrees {
d, err := ParseDegrees(s)
if err != nil {
panic(err)
}
return d
}
// String implements the [Stringer] interface. The output is formatted in
// decimal degrees, prefixed by either the appropriate + or - sign, and suffixed
// by a ° degree symbol.
func (d Degrees) String() string {
b, _ := d.AppendText(nil)
b = append(b, []byte("°")...)
return string(b)
}
// AppendText implements [encoding.TextAppender]. The output is formatted in
// decimal degrees, prefixed by either the appropriate + or - sign.
func (d Degrees) AppendText(b []byte) ([]byte, error) {
b = d.AppendZeroPaddedText(b, 0)
return b, nil
}
// AppendZeroPaddedText appends d formatted as decimal degrees to b. The number of
// integer digits will be zero-padded to nint.
func (d Degrees) AppendZeroPaddedText(b []byte, nint int) []byte {
n := float64(d)
if math.IsInf(n, 0) || math.IsNaN(n) {
return strconv.AppendFloat(b, n, 'f', -1, 64)
}
sign := byte('+')
if math.Signbit(n) {
sign = '-'
n = -n
}
b = append(b, sign)
pad := nint - 1
for nn := n / 10; nn >= 1 && pad > 0; nn /= 10 {
pad--
}
for range pad {
b = append(b, '0')
}
return strconv.AppendFloat(b, n, 'f', -1, 64)
}
// Radians converts d into radians.
func (d Degrees) Radians() Radians {
return Radians(d * math.Pi / 180.0)
}
// Turns converts d into a number of turns.
func (d Degrees) Turns() Turns {
return Turns(d / 360.0)
}
// Radians represents a latitude or longitude, in radians.
type Radians float64
// ParseRadians parses s as radians.
func ParseRadians(s string) (Radians, error) {
s = strings.TrimSuffix(s, "rad")
s = strings.TrimRightFunc(s, unicode.IsSpace)
f, err := strconv.ParseFloat(s, 64)
return Radians(f), err
}
// MustParseRadians parses s as radians, but panics on error.
func MustParseRadians(s string) Radians {
r, err := ParseRadians(s)
if err != nil {
panic(err)
}
return r
}
// String implements the [Stringer] interface.
func (r Radians) String() string {
return strconv.FormatFloat(float64(r), 'f', -1, 64) + " rad"
}
// Degrees converts r into decimal degrees.
func (r Radians) Degrees() Degrees {
return Degrees(r * 180.0 / math.Pi)
}
// Turns converts r into a number of turns.
func (r Radians) Turns() Turns {
return Turns(r / 2 / math.Pi)
}
// Turns represents a number of complete revolutions around a sphere.
type Turns float64
// String implements the [Stringer] interface.
func (o Turns) String() string {
return strconv.FormatFloat(float64(o), 'f', -1, 64)
}
// Degrees converts t into decimal degrees.
func (o Turns) Degrees() Degrees {
return Degrees(o * 360.0)
}
// Radians converts t into radians.
func (o Turns) Radians() Radians {
return Radians(o * 2 * math.Pi)
}
// Distance represents a great-circle distance in meters.
type Distance float64
// ParseDistance parses s as distance in meters.
func ParseDistance(s string) (Distance, error) {
s = strings.TrimSuffix(s, "m")
s = strings.TrimRightFunc(s, unicode.IsSpace)
f, err := strconv.ParseFloat(s, 64)
return Distance(f), err
}
// MustParseDistance parses s as distance in meters, but panics on error.
func MustParseDistance(s string) Distance {
d, err := ParseDistance(s)
if err != nil {
panic(err)
}
return d
}
// String implements the [Stringer] interface.
func (d Distance) String() string {
return strconv.FormatFloat(float64(d), 'f', -1, 64) + "m"
}
// DistanceOnEarth converts t turns into the great-circle distance, in meters.
func DistanceOnEarth(t Turns) Distance {
return Distance(t) * EarthMeanCircumference
}
// Earth Fact Sheet
// https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
const (
// EarthMeanRadius is the volumetric mean radius of the Earth.
EarthMeanRadius = 6_371_000 * Meter
// EarthMeanCircumference is the volumetric mean circumference of the Earth.
EarthMeanCircumference = 2 * math.Pi * EarthMeanRadius
// earthEquatorialRadius is the equatorial radius of the Earth.
earthEquatorialRadius = 6_378_137 * Meter
// earthEquatorialCircumference is the equatorial circumference of the Earth.
earthEquatorialCircumference = 2 * math.Pi * earthEquatorialRadius
// earthPolarRadius is the polar radius of the Earth.
earthPolarRadius = 6_356_752 * Meter
// earthPolarCircumference is the polar circumference of the Earth.
earthPolarCircumference = 2 * math.Pi * earthPolarRadius
)