Settings > Usage > "Day starts at" (dayStart, 0 to 6, default 0; also magpie settings set dayStart 4). With 4, a call at 01:30 counts toward the day before: the Usage page (summary, charts, heatmap, ledger day filter, hour labels of Today) and the daily and monthly key caps (access.Window, provider.SpendCap.Window). internal/dayzone makes a time.Location whose midnight is the real day's hour:00 (TZif built from the zone's own offsets, same DST instants), so every place that cuts days by a Zone's midnight follows without edits; usage.Zone returns it and the "now" reads use it too. Run (mote): go test usage, settings, settingsapi, access, provider, gui, dayzone, also with TZ=UTC and TZ=Pacific/Kiritimati; gofmt; go vet darwin, linux nogui and windows; day-start, currency, number-units and i18n-keys GUI files in chromium and webkit, en zh zh-TW ja de; TestCoreScriptBudget. Broken by hand: TestDayStartsAfterMidnight red with 9 failures when the hour is ignored. New tests: dayzone over 5 zones x hours 1/4/6 x two years of days, usage, limit window, spend-cap window, day-start GUI. Not tried: a real log history in a real window. Not shifted, filed as #6314: the Sessions page, TUI sessions filter, davsync shared per-day files, gateway_sessions day keys, credits_daily. Rows are never stored in the shifted zone.
61 lines
2.1 KiB
Go
61 lines
2.1 KiB
Go
package dayzone
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func mustLoad(t *testing.T, name string) *time.Location {
|
|
t.Helper()
|
|
l, err := time.LoadLocation(name)
|
|
if err != nil {
|
|
t.Skipf("no zone data for %s: %v", name, err)
|
|
}
|
|
return l
|
|
}
|
|
|
|
// The day starts at the hour: a call a minute before it belongs to the day
|
|
// before, the hour itself to the new day, in a zone without daylight saving
|
|
// and in ones that have it (north and south), across each of their changes.
|
|
func TestDayStartsAtTheHour(t *testing.T) {
|
|
for _, name := range []string{"Asia/Shanghai", "America/New_York", "Australia/Sydney", "Pacific/Kiritimati", "UTC"} {
|
|
loc := mustLoad(t, name)
|
|
for _, hour := range []int{1, 4, 6} {
|
|
sh := Shift(loc, hour)
|
|
if Shift(loc, hour) != sh {
|
|
t.Fatalf("%s %d: not the same location twice", name, hour)
|
|
}
|
|
if got := Offset(sh); got != time.Duration(hour)*time.Hour {
|
|
t.Fatalf("%s %d: Offset = %v", name, hour, got)
|
|
}
|
|
y := time.Now().Year()
|
|
for d := time.Date(y-1, 1, 1, 12, 0, 0, 0, loc); d.Year() <= y+1; d = d.AddDate(0, 0, 1) {
|
|
start := time.Date(d.Year(), d.Month(), d.Day(), hour, 0, 0, 0, loc)
|
|
if start.Hour() != hour {
|
|
continue // that wall hour is skipped on this date
|
|
}
|
|
day := time.Date(d.Year(), d.Month(), d.Day(), 12, 0, 0, 0, time.UTC)
|
|
before, after := start.Add(-time.Minute).In(sh), start.In(sh)
|
|
if before.Format(time.DateOnly) != day.AddDate(0, 0, -1).Format(time.DateOnly) {
|
|
t.Fatalf("%s h%d: %v is in %s, want the day before %s", name, hour, start.Add(-time.Minute), before.Format(time.DateOnly), day.Format(time.DateOnly))
|
|
}
|
|
if after.Format(time.DateOnly) != day.Format(time.DateOnly) || after.Hour() != 0 || after.Minute() != 0 {
|
|
t.Fatalf("%s h%d: day start %v reads %v in the shifted zone", name, hour, start, after)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNoShiftIsTheZoneItself(t *testing.T) {
|
|
loc := mustLoad(t, "Asia/Shanghai")
|
|
for _, h := range []int{-1, 0, MaxStart + 1} {
|
|
if Shift(loc, h) != loc {
|
|
t.Fatalf("hour %d shifted", h)
|
|
}
|
|
}
|
|
if Offset(loc) != 0 {
|
|
t.Fatal("a plain zone has an offset")
|
|
}
|
|
}
|