Manager: fix sleep scheduler time zone handling

The sleep scheduler now always works in the local time zone.

This fixes the sleep scheduler part of #104219.
This commit is contained in:
Sybren A. Stüvel 2023-08-23 13:51:24 +00:00
parent ef726da17b
commit eb9f46dc9b
2 changed files with 7 additions and 5 deletions

@ -60,7 +60,7 @@ func calculateNextCheck(now time.Time, schedule *persistence.SleepSchedule) time
// calcNext returns the given time of day on "today" if that hasn't passed
// yet, otherwise on "tomorrow".
calcNext := func(tod persistence.TimeOfDay) time.Time {
nextCheck := tod.OnDate(now)
nextCheck := tod.OnDate(now).In(time.Local)
if nextCheck.Before(now) {
nextCheck = nextCheck.AddDate(0, 0, 1)
}
@ -99,5 +99,6 @@ func earliestTime(timestamps []time.Time) time.Time {
// endOfDay returns the next midnight at UTC.
func endOfDay(now time.Time) time.Time {
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, 1)
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
return startOfDay.AddDate(0, 0, 1)
}

@ -256,13 +256,14 @@ type TestMocks struct {
// to the given time. Seconds and sub-seconds are set to zero.
func (m *TestMocks) todayAt(hour, minute int) time.Time {
now := m.clock.Now()
return time.Date(now.Year(), now.Month(), now.Day(), hour, minute, 0, 0, now.Location())
todayAt := time.Date(now.Year(), now.Month(), now.Day(), hour, minute, 0, 0, time.Local)
return todayAt
}
// endOfDay returns midnight of the day after whatever the mocked clock's "now" is set to.
func (m *TestMocks) endOfDay() time.Time {
now := m.clock.Now().UTC()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, 1)
startOfToday := m.todayAt(0, 0)
return startOfToday.AddDate(0, 0, 1)
}
func testFixtures(t *testing.T) (*SleepScheduler, TestMocks, context.Context) {