@@ -317,21 +317,22 @@ func (w *Location) updateForecast(ctx context.Context) (Location, error) {
317
317
if err != nil {
318
318
return * w , err
319
319
}
320
- w .Forecast = processForecast (newForecast )
320
+ w .Forecast = processForecast (newForecast , w . Weather . Timezone )
321
321
return * w , nil
322
322
}
323
323
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 )
328
329
329
330
daily := make (map [string ]* DailySummary )
330
331
weatherCounts := make (map [string ]map [int ]int )
331
332
332
333
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 )
335
336
336
337
// Skip today and past
337
338
if ! itemDate .After (today ) {
@@ -342,11 +343,16 @@ func processForecast(forecast Forecast) []DailySummary {
342
343
343
344
// Init if not exists
344
345
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
+ }
345
351
daily [dateStr ] = & DailySummary {
346
352
Date : itemDate ,
347
353
DateStr : dateStr ,
348
354
MaxTemp : item .Main .TempMax ,
349
- WeatherIcon : item . Data [ 0 ]. ID ,
355
+ WeatherIcon : iconID ,
350
356
}
351
357
weatherCounts [dateStr ] = make (map [int ]int )
352
358
}
@@ -357,7 +363,10 @@ func processForecast(forecast Forecast) []DailySummary {
357
363
}
358
364
359
365
// 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
+ }
361
370
weatherCounts [dateStr ][weatherID ]++
362
371
363
372
// Update most common
@@ -381,6 +390,7 @@ func processForecast(forecast Forecast) []DailySummary {
381
390
return summaries [i ].DateStr < summaries [j ].DateStr
382
391
})
383
392
384
- return summaries [:3 ]
393
+ n := min (3 , len (summaries ))
394
+ return summaries [:n ]
385
395
386
396
}
0 commit comments