Skip to content

Commit 037e3ae

Browse files
committed
a bit more safty
1 parent 5b447e5 commit 037e3ae

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

internal/templates/partials/weather.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ templ WeatherLocation(weatherData weather.Location, systemLang monday.Locale) {
108108
<div class="weather--forecast">
109109
for _, f := range weatherData.Forecast {
110110
{{
111-
forecastDay := cases.Title(language.Und).String(monday.Format(f.Date, "Mon", systemLang))
111+
forecastDay := monday.Format(f.Date, "Mon", systemLang)
112112
}}
113113
<div class="weather--forecast--item">
114114
<div>{ forecastDay }</div>

internal/weather/weather.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,22 @@ func (w *Location) updateForecast(ctx context.Context) (Location, error) {
317317
if err != nil {
318318
return *w, err
319319
}
320-
w.Forecast = processForecast(newForecast)
320+
w.Forecast = processForecast(newForecast, w.Weather.Timezone)
321321
return *w, nil
322322
}
323323

324-
func processForecast(forecast Forecast) []DailySummary {
325-
// Today’s date at midnight UTC
326-
now := time.Now().UTC()
327-
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
324+
func processForecast(forecast Forecast, tzOffsetSeconds int) []DailySummary {
325+
loc := time.FixedZone("owm", tzOffsetSeconds)
326+
// Today’s date at midnight in location zone
327+
now := time.Now().In(loc)
328+
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
328329

329330
daily := make(map[string]*DailySummary)
330331
weatherCounts := make(map[string]map[int]int)
331332

332333
for _, item := range forecast.List {
333-
itemTime := time.Unix(item.DT, 0).UTC()
334-
itemDate := time.Date(itemTime.Year(), itemTime.Month(), itemTime.Day(), 0, 0, 0, 0, time.UTC)
334+
itemTime := time.Unix(item.DT, 0).In(loc)
335+
itemDate := time.Date(itemTime.Year(), itemTime.Month(), itemTime.Day(), 0, 0, 0, 0, loc)
335336

336337
// Skip today and past
337338
if !itemDate.After(today) {
@@ -342,11 +343,16 @@ func processForecast(forecast Forecast) []DailySummary {
342343

343344
// Init if not exists
344345
if _, ok := daily[dateStr]; !ok {
346+
// Default to clear sky (800) if no descriptors
347+
iconID := 800
348+
if len(item.Data) > 0 {
349+
iconID = item.Data[0].ID
350+
}
345351
daily[dateStr] = &DailySummary{
346352
Date: itemDate,
347353
DateStr: dateStr,
348354
MaxTemp: item.Main.TempMax,
349-
WeatherIcon: item.Data[0].ID,
355+
WeatherIcon: iconID,
350356
}
351357
weatherCounts[dateStr] = make(map[int]int)
352358
}
@@ -357,7 +363,10 @@ func processForecast(forecast Forecast) []DailySummary {
357363
}
358364

359365
// Count weather.id
360-
weatherID := item.Data[0].ID
366+
weatherID := daily[dateStr].WeatherIcon
367+
if len(item.Data) > 0 {
368+
weatherID = item.Data[0].ID
369+
}
361370
weatherCounts[dateStr][weatherID]++
362371

363372
// Update most common
@@ -381,6 +390,7 @@ func processForecast(forecast Forecast) []DailySummary {
381390
return summaries[i].DateStr < summaries[j].DateStr
382391
})
383392

384-
return summaries[:3]
393+
n := min(3, len(summaries))
394+
return summaries[:n]
385395

386396
}

0 commit comments

Comments
 (0)