2026-03-17 08:35:16 -05:00
|
|
|
package units
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/ejr/weatherapi/internal/platform/constants"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Converter interface {
|
|
|
|
|
TemperatureCToOutput(celsius *float64) *float64
|
|
|
|
|
SpeedKmhToOutput(kmh *float64) *float64
|
2026-03-17 15:51:00 -05:00
|
|
|
System() string
|
2026-03-17 08:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type USConverter struct{}
|
|
|
|
|
|
2026-03-17 15:51:00 -05:00
|
|
|
func (USConverter) System() string {
|
|
|
|
|
return "us"
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 08:35:16 -05:00
|
|
|
func (USConverter) TemperatureCToOutput(celsius *float64) *float64 {
|
|
|
|
|
if celsius == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
f := round(constants.CelsiusToFahrenheit(*celsius), constants.TempFahrenheitPrecision)
|
|
|
|
|
return &f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (USConverter) SpeedKmhToOutput(kmh *float64) *float64 {
|
|
|
|
|
if kmh == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
mph := round(constants.KmhToMph(*kmh), constants.WindMphPrecision)
|
|
|
|
|
return &mph
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 15:51:00 -05:00
|
|
|
type MetricConverter struct{}
|
|
|
|
|
|
|
|
|
|
func (MetricConverter) System() string {
|
|
|
|
|
return "metric"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (MetricConverter) TemperatureCToOutput(celsius *float64) *float64 {
|
|
|
|
|
return clone(celsius)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (MetricConverter) SpeedKmhToOutput(kmh *float64) *float64 {
|
|
|
|
|
return clone(kmh)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func clone(v *float64) *float64 {
|
|
|
|
|
if v == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
out := *v
|
|
|
|
|
return &out
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 08:35:16 -05:00
|
|
|
func round(v float64, precision int) float64 {
|
|
|
|
|
pow := math.Pow(10, float64(precision))
|
|
|
|
|
return math.Round(v*pow) / pow
|
|
|
|
|
}
|