package timeutil import ( "testing" "time" ) func TestFormatAPITimeUsesBusinessTimezone(t *testing.T) { value := time.Date(2026, 7, 30, 9, 53, 58, 147355000, time.UTC) if got := FormatAPITime(value); got != "2026-07-30T17:53:58+08:00" { t.Fatalf("unexpected api time: %s", got) } } func TestStartOfDayUsesBusinessTimezone(t *testing.T) { value := time.Date(2026, 7, 30, 18, 30, 0, 0, time.UTC) got := StartOfDay(value) if got.Format(APITimeLayout) != "2026-07-31T00:00:00+08:00" { t.Fatalf("unexpected day start: %s", got.Format(APITimeLayout)) } } // TestLocalZoneNotice 锁住启动体检:进程时区偏移不等于 +08:00 时必须给出提示, // 否则 pgx 把 timestamptz 解码到 time.Local 会让接口返回的时间偏移悄悄变化。 func TestLocalZoneNotice(t *testing.T) { original := time.Local t.Cleanup(func() { time.Local = original }) time.Local = time.FixedZone("CST", BusinessZoneOffsetSeconds) if got := LocalZoneNotice(); got != "" { t.Fatalf("业务时区不应告警,实际 %q", got) } time.Local = time.UTC if got := LocalZoneNotice(); got == "" { t.Fatal("UTC 进程时区必须告警") } time.Local = time.FixedZone("EST", -5*60*60) if got := LocalZoneNotice(); got == "" { t.Fatal("负偏移进程时区必须告警") } }