Add mph and km units
This commit is contained in:
parent
2c16694cec
commit
54209e2191
@ -19,8 +19,8 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
pretty := pflag.BoolP("pretty", "p", true, "Pretty-print the JSON output")
|
pretty := pflag.BoolP("pretty", "p", true, "Pretty-print the JSON output")
|
||||||
printGo := pflag.BoolP("print-go", "G", false, "Print Go code instead of JSON")
|
printGo := pflag.BoolP("print-go", "G", false, "Print Go code instead of JSON")
|
||||||
convertDist := pflag.StringP("convert-distance", "d", "", "Convert all the distances to the given unit. (valid units: sm, mi, m)")
|
convertDist := pflag.StringP("convert-distance", "d", "", "Convert all the distances to the given unit. (valid units: mi, m, km)")
|
||||||
convertSpd := pflag.StringP("convert-speed", "s", "", "Convert all the speeds to the given unit. (valid units: m/s, mps, kmh, kph, kt, kts)")
|
convertSpd := pflag.StringP("convert-speed", "s", "", "Convert all the speeds to the given unit. (valid units: m/s, kph, kts, mph)")
|
||||||
pflag.Parse()
|
pflag.Parse()
|
||||||
|
|
||||||
var opts taf.Options
|
var opts taf.Options
|
||||||
|
@ -12,6 +12,7 @@ const (
|
|||||||
MetersPerSecond Speed = "MetersPerSecond"
|
MetersPerSecond Speed = "MetersPerSecond"
|
||||||
KilometersPerHour Speed = "KilometersPerHour"
|
KilometersPerHour Speed = "KilometersPerHour"
|
||||||
Knots Speed = "Knots"
|
Knots Speed = "Knots"
|
||||||
|
MilesPerHour Speed = "MilesPerHour"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Convert converts a value from one unit to another
|
// Convert converts a value from one unit to another
|
||||||
@ -29,22 +30,36 @@ func (sf Speed) Convert(st Speed, val int) int {
|
|||||||
return int(float64(val) * 1.94384)
|
return int(float64(val) * 1.94384)
|
||||||
case sf == Knots && st == MetersPerSecond:
|
case sf == Knots && st == MetersPerSecond:
|
||||||
return int(float64(val) / 1.94384)
|
return int(float64(val) / 1.94384)
|
||||||
|
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)
|
||||||
default:
|
default:
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseSpeed parses a speed value. Valid inputs include:
|
// ParseSpeed parses a speed value. Valid inputs include:
|
||||||
// mps, m/s, kmh, kph, kt, and kts.
|
// mps, m/s, kmh, kph, kt, kts, mph, and milesperhour.
|
||||||
// This function is case-insensitive.
|
// This function is case-insensitive.
|
||||||
func ParseSpeed(s string) (Speed, bool) {
|
func ParseSpeed(s string) (Speed, bool) {
|
||||||
switch strings.ToLower(s) {
|
switch strings.ToLower(s) {
|
||||||
case "m/s", "mps", "meterspersecond", "meters per second":
|
case "m/s", "mps", "meterspersecond", "meters per second", "metrespersecond", "metres per second":
|
||||||
return MetersPerSecond, true
|
return MetersPerSecond, true
|
||||||
case "kmh", "kph", "km/h", "kilometersperhour", "kilometers per hour":
|
case "kmh", "kph", "km/h", "kilometersperhour", "kilometers per hour", "kilometresperhour", "kilometres per hour":
|
||||||
return KilometersPerHour, true
|
return KilometersPerHour, true
|
||||||
case "kt", "kts", "knot", "knots":
|
case "kt", "kts", "knot", "knots":
|
||||||
return Knots, true
|
return Knots, true
|
||||||
|
case "mph", "milesperhour", "miles per hour":
|
||||||
|
return MilesPerHour, true
|
||||||
default:
|
default:
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
@ -55,8 +70,9 @@ type Distance string
|
|||||||
|
|
||||||
// Distance units
|
// Distance units
|
||||||
const (
|
const (
|
||||||
Miles Distance = "Miles"
|
Miles Distance = "Miles"
|
||||||
Meters Distance = "Meters"
|
Meters Distance = "Meters"
|
||||||
|
Kilometers Distance = "Kilometers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Convert converts a value from one unit to another
|
// Convert converts a value from one unit to another
|
||||||
@ -66,19 +82,30 @@ func (df Distance) Convert(dt Distance, val float64) float64 {
|
|||||||
return val * 1609
|
return val * 1609
|
||||||
case df == Meters && dt == Miles:
|
case df == Meters && dt == Miles:
|
||||||
return val / 1609
|
return val / 1609
|
||||||
|
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
|
||||||
default:
|
default:
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseDistance parses a speed value. Valid inputs include:
|
// ParseDistance parses a distance value. Valid inputs include:
|
||||||
// sm, mi, and m. This function is case-insensitive.
|
// sm, mi, m, km, and kilometers.
|
||||||
|
// This function is case-insensitive.
|
||||||
func ParseDistance(s string) (Distance, bool) {
|
func ParseDistance(s string) (Distance, bool) {
|
||||||
switch strings.ToLower(s) {
|
switch strings.ToLower(s) {
|
||||||
case "sm", "mi", "mile", "miles":
|
case "sm", "mi", "mile", "miles":
|
||||||
return Miles, true
|
return Miles, true
|
||||||
case "m", "meter", "meters":
|
case "m", "meter", "meters", "metre", "metres":
|
||||||
return Meters, true
|
return Meters, true
|
||||||
|
case "km", "kilometer", "kilometers", "kilometre", "kilometres":
|
||||||
|
return Kilometers, true
|
||||||
default:
|
default:
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user