bugfix wunderground reporting

This commit is contained in:
2026-03-09 09:19:45 +11:00
parent 5b8cad905f
commit c796f1324e
12 changed files with 253 additions and 33 deletions

View File

@@ -27,7 +27,9 @@ type Latest struct {
// rolling sums built from "rain increment" values (mm)
rainIncs []rainIncPoint // last 1h
dailyIncs []rainIncPoint // since midnight (or since start; well trim daily by midnight)
dailyIncs []rainIncPoint // since midnight in rainDayLoc (or since start; trimmed each update)
rainDayLoc *time.Location
}
type rainIncPoint struct {
@@ -35,6 +37,15 @@ type rainIncPoint struct {
mm float64 // incremental rainfall at this timestamp (mm)
}
func NewLatest(rainDayLoc *time.Location) *Latest {
if rainDayLoc == nil {
rainDayLoc = time.Local
}
return &Latest{
rainDayLoc: rainDayLoc,
}
}
func (l *Latest) Update(ts time.Time, p *WS90Payload) {
l.mu.Lock()
defer l.mu.Unlock()
@@ -49,9 +60,9 @@ func (l *Latest) Update(ts time.Time, p *WS90Payload) {
cutoff := ts.Add(-1 * time.Hour)
l.rainIncs = trimBefore(l.rainIncs, cutoff)
// Track daily increments: trim before local midnight
// Track daily increments: trim before midnight in configured rain day timezone.
l.dailyIncs = append(l.dailyIncs, rainIncPoint{ts: ts, mm: inc})
midnight := localMidnight(ts)
midnight := l.rainDayMidnight(ts)
l.dailyIncs = trimBefore(l.dailyIncs, midnight)
}
@@ -68,10 +79,13 @@ func trimBefore(a []rainIncPoint, cutoff time.Time) []rainIncPoint {
return a
}
// localMidnight returns midnight in the local timezone of the *process*.
// If you want a specific timezone (e.g. Australia/Sydney) we can wire that in later.
func localMidnight(t time.Time) time.Time {
lt := t.Local()
// rainDayMidnight returns midnight in the configured rain day timezone.
func (l *Latest) rainDayMidnight(t time.Time) time.Time {
loc := l.rainDayLoc
if loc == nil {
loc = time.Local
}
lt := t.In(loc)
return time.Date(lt.Year(), lt.Month(), lt.Day(), 0, 0, 0, 0, lt.Location())
}