taf/units/units.go

113 lines
3.2 KiB
Go
Raw Normal View History

2023-08-21 23:56:17 +00:00
package units
import (
"strings"
)
// Speed represents a unit of speed
type Speed string
// Speed units
const (
MetersPerSecond Speed = "MetersPerSecond"
KilometersPerHour Speed = "KilometersPerHour"
Knots Speed = "Knots"
2023-08-23 02:52:41 +00:00
MilesPerHour Speed = "MilesPerHour"
2023-08-21 23:56:17 +00:00
)
// Convert converts a value from one unit to another
func (sf Speed) Convert(st Speed, val int) int {
switch {
case sf == MetersPerSecond && st == KilometersPerHour:
return int(float64(val) * 3.6)
case sf == KilometersPerHour && st == MetersPerSecond:
return int(float64(val) / 3.6)
case sf == Knots && st == KilometersPerHour:
return int(float64(val) * 1.852)
case sf == KilometersPerHour && st == Knots:
return int(float64(val) / 1.852)
case sf == MetersPerSecond && st == Knots:
return int(float64(val) * 1.94384)
case sf == Knots && st == MetersPerSecond:
return int(float64(val) / 1.94384)
2023-08-23 02:52:41 +00:00
case sf == MilesPerHour && st == KilometersPerHour:
return int(float64(val) * 1.60934)
case sf == KilometersPerHour && st == MilesPerHour:
return int(float64(val) / 1.60934)
case sf == MilesPerHour && st == MetersPerSecond:
return int(float64(val) * 0.44704)
case sf == MetersPerSecond && st == MilesPerHour:
return int(float64(val) / 0.44704)
case sf == MilesPerHour && st == Knots:
return int(float64(val) * 0.868976)
case sf == Knots && st == MilesPerHour:
return int(float64(val) / 0.868976)
2023-08-21 23:56:17 +00:00
default:
return val
}
}
// ParseSpeed parses a speed value. Valid inputs include:
2023-08-23 02:52:41 +00:00
// mps, m/s, kmh, kph, kt, kts, mph, and milesperhour.
2023-08-21 23:56:17 +00:00
// This function is case-insensitive.
func ParseSpeed(s string) (Speed, bool) {
switch strings.ToLower(s) {
2023-08-23 02:52:41 +00:00
case "m/s", "mps", "meterspersecond", "meters per second", "metrespersecond", "metres per second":
return MetersPerSecond, true
2023-08-23 02:52:41 +00:00
case "kmh", "kph", "km/h", "kilometersperhour", "kilometers per hour", "kilometresperhour", "kilometres per hour":
return KilometersPerHour, true
case "kt", "kts", "knot", "knots":
return Knots, true
2023-08-23 02:52:41 +00:00
case "mph", "milesperhour", "miles per hour":
return MilesPerHour, true
default:
return "", false
2023-08-21 23:56:17 +00:00
}
}
// Distance represents a unit of distance
type Distance string
// Distance units
const (
2023-08-23 02:52:41 +00:00
Miles Distance = "Miles"
Meters Distance = "Meters"
Kilometers Distance = "Kilometers"
2023-08-21 23:56:17 +00:00
)
// Convert converts a value from one unit to another
func (df Distance) Convert(dt Distance, val float64) float64 {
switch {
case df == Miles && dt == Meters:
2023-08-21 23:56:17 +00:00
return val * 1609
case df == Meters && dt == Miles:
2023-08-21 23:56:17 +00:00
return val / 1609
2023-08-23 02:52:41 +00:00
case df == Kilometers && dt == Meters:
return val * 1000
case df == Meters && dt == Kilometers:
return val / 1000
case df == Miles && dt == Kilometers:
return val * 1.60934
case df == Kilometers && dt == Miles:
return val / 1.60934
2023-08-21 23:56:17 +00:00
default:
return val
}
}
2023-08-23 02:52:41 +00:00
// ParseDistance parses a distance value. Valid inputs include:
// sm, mi, m, km, and kilometers.
// This function is case-insensitive.
2023-08-21 23:56:17 +00:00
func ParseDistance(s string) (Distance, bool) {
switch strings.ToLower(s) {
case "sm", "mi", "mile", "miles":
return Miles, true
2023-08-23 02:52:41 +00:00
case "m", "meter", "meters", "metre", "metres":
return Meters, true
2023-08-23 02:52:41 +00:00
case "km", "kilometer", "kilometers", "kilometre", "kilometres":
return Kilometers, true
default:
return "", false
2023-08-21 23:56:17 +00:00
}
}