@@ -34,6 +34,26 @@ func mapUnit(unit string) (string, error) {
3434 return "" , fmt .Errorf ("unit must be one of 'imperial', 'metric', or 'uk hybrid'; not %q" , unit )
3535}
3636
37+ func mapWindSpeedUnit (unit string ) string {
38+ switch unit {
39+ case "e" , "h" :
40+ return "mph"
41+ case "m" :
42+ return "km/h"
43+ }
44+ return ""
45+ }
46+
47+ func mapTemperatureUnit (unit string ) string {
48+ switch unit {
49+ case "e" :
50+ return "°F"
51+ case "m" , "h" :
52+ return "°C"
53+ }
54+ return ""
55+ }
56+
3757func GetDailyForecast (ctx context.Context , lat , lon float64 , units string ) (* Forecast , error ) {
3858 units , err := mapUnit (units )
3959 if err != nil {
@@ -52,6 +72,8 @@ func GetDailyForecast(ctx context.Context, lat, lon float64, units string) (*For
5272 if err := json .NewDecoder (resp .Body ).Decode (& forecast ); err != nil {
5373 return nil , fmt .Errorf ("error decoding response: %w" , err )
5474 }
75+ forecast .WindSpeedUnit = mapWindSpeedUnit (units )
76+ forecast .TemperatureUnit = mapTemperatureUnit (units )
5577 return & forecast , nil
5678}
5779
@@ -73,6 +95,8 @@ func GetCurrentConditions(ctx context.Context, lat, lon float64, units string) (
7395 if err := json .NewDecoder (resp .Body ).Decode (& conditions ); err != nil {
7496 return nil , fmt .Errorf ("error decoding response: %w" , err )
7597 }
98+ conditions .WindSpeedUnit = mapWindSpeedUnit (units )
99+ conditions .TemperatureUnit = mapTemperatureUnit (units )
76100 return & conditions , nil
77101}
78102
@@ -94,6 +118,7 @@ func GetHourlyForecast(ctx context.Context, lat, lon float64, units string) (*Ho
94118 if err := json .NewDecoder (resp .Body ).Decode (& hourly ); err != nil {
95119 return nil , fmt .Errorf ("error decoding response: %w" , err )
96120 }
121+ hourly .TemperatureUnit = mapTemperatureUnit (units )
97122 return & hourly , nil
98123}
99124
@@ -112,6 +137,8 @@ type Forecast struct {
112137 Qpf []float32 `json:"qpf"`
113138 QpfSnow []float32 `json:"qpfSnow"`
114139 DayParts []ForecastDayPart `json:"daypart"`
140+ WindSpeedUnit string `json:"windSpeedUnit"`
141+ TemperatureUnit string `json:"temperatureUnit"`
115142}
116143
117144type ForecastDayPart struct {
@@ -160,20 +187,23 @@ type CurrentConditions struct {
160187 TemperatureMax24Hour int `json:"temperatureMax24Hour"`
161188 TemperatureMin24Hour int `json:"temperatureMin24Hour"`
162189 TemperatureWindChill int `json:"temperatureWindChill"`
190+ TemperatureUnit string `json:"temperatureUnit"`
163191 UVIndex int `json:"uvIndex"`
164192 Visibility float32 `json:"visibility"`
165193 WindDirectionCardinal string `json:"windDirectionCardinal"`
166194 WindSpeed int `json:"windSpeed"`
195+ WindSpeedUnit string `json:"windSpeedUnit"`
167196 WindGust int `json:"windGust"`
168197 Description string `json:"wxPhraseLong"`
169198 IconCode int `json:"iconCode"`
170199}
171200
172201type HourlyForecast struct {
173- WxPhraseLong []string `json:"wxPhraseLong"`
174- Temperature []int `json:"temperature"`
175- PrecipChance []int `json:"precipChance"`
176- PrecipType []string `json:"precipType"`
177- ValidTimeLocal []string `json:"validTimeLocal"`
178- UVIndex []int `json:"uvIndex"`
202+ WxPhraseLong []string `json:"wxPhraseLong"`
203+ Temperature []int `json:"temperature"`
204+ PrecipChance []int `json:"precipChance"`
205+ PrecipType []string `json:"precipType"`
206+ ValidTimeLocal []string `json:"validTimeLocal"`
207+ UVIndex []int `json:"uvIndex"`
208+ TemperatureUnit string `json:"temperatureUnit"`
179209}
0 commit comments