Preserve civil daypart clocks across DST

This commit is contained in:
2026-08-13 00:39:46 +00:00
parent 8bb7307f22
commit 8fafacf921
4 changed files with 116 additions and 4 deletions

View File

@@ -72,10 +72,17 @@ func CivilDay(date time.Time, location *time.Location) Period {
func ClockWindow(date time.Time, location *time.Location, startClock time.Duration, endClock time.Duration) Period {
day := CivilDay(date, location)
start := day.Start.Add(startClock)
end := day.Start.Add(endClock)
start := civilClock(day.Start, location, startClock)
end := civilClock(day.Start, location, endClock)
if !end.After(start) {
end = end.AddDate(0, 0, 1)
end = civilClock(day.Start.AddDate(0, 0, 1), location, endClock)
}
return Period{Start: start, End: end}
}
func civilClock(date time.Time, location *time.Location, clock time.Duration) time.Time {
local := date.In(location)
hour := int(clock / time.Hour)
minute := int(clock%time.Hour) / int(time.Minute)
return time.Date(local.Year(), local.Month(), local.Day(), hour, minute, 0, 0, location)
}