Add forecast daypart derivation
This commit is contained in:
53
internal/forecast/dayparts.go
Normal file
53
internal/forecast/dayparts.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package forecast
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/timeutil"
|
||||
)
|
||||
|
||||
type DaypartDefinition struct {
|
||||
Name string `json:"name"`
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
}
|
||||
|
||||
type DaypartWindow struct {
|
||||
Name string `json:"name"`
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
Period timeutil.Period `json:"period"`
|
||||
}
|
||||
|
||||
func ResolveDayparts(date time.Time, location *time.Location, definitions []DaypartDefinition) ([]DaypartWindow, error) {
|
||||
if len(definitions) == 0 {
|
||||
return nil, fmt.Errorf("daypart definitions are required")
|
||||
}
|
||||
windows := make([]DaypartWindow, 0, len(definitions))
|
||||
for i, def := range definitions {
|
||||
if def.Name == "" {
|
||||
return nil, fmt.Errorf("daypart[%d].name is required", i)
|
||||
}
|
||||
startClock, err := timeutil.ParseClock(def.Start)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("daypart[%d].start: %w", i, err)
|
||||
}
|
||||
endClock, err := timeutil.ParseClock(def.End)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("daypart[%d].end: %w", i, err)
|
||||
}
|
||||
period := timeutil.ClockWindow(date, location, startClock, endClock)
|
||||
windows = append(windows, DaypartWindow{
|
||||
Name: def.Name,
|
||||
Start: period.Start,
|
||||
End: period.End,
|
||||
Period: period,
|
||||
})
|
||||
}
|
||||
return windows, nil
|
||||
}
|
||||
|
||||
func PeriodForForecastPeriod(period ForecastPeriod) timeutil.Period {
|
||||
return timeutil.Period{Start: period.StartTime, End: period.EndTime}
|
||||
}
|
||||
Reference in New Issue
Block a user