Repository navigation
fix(duration): truncate hours on local boundaries in fractional-offset zones - #10187
Tyagiquamar wants to merge 4 commits into
Conversation
nishantmonu51
left a comment
There was a problem hiding this comment.
One DST case inline.
| return t.Truncate(time.Hour).Add(-time.Duration(n) * time.Hour) | ||
| // Build the boundary from local clock fields: time.Truncate aligns to UTC | ||
| // hour boundaries, which are not on the hour in zones with a fractional offset. | ||
| return time.Date(t.Year(), t.Month(), t.Day(), t.Hour()-n, 0, 0, 0, t.Location()) |
There was a problem hiding this comment.
time.Date can pick the other occurrence of a repeated hour. I checked 2023-10-29T00:15:00Z in Copenhagen: the old PT1H floor was 02:00 CEST, but this returns 02:00 CET, 45 minutes after the input. calculateReportExecutionTimes and calculateAlertExecutionTimes use this as the last closed interval, so they can run an hour too early. Could we keep the input’s offset when both occurrences match the target local hour? Construct both fold cases from explicit UTC instants in TestTruncate so the test exercises both offsets.
|
@nishantmonu51 Good catch. Updated Truncate in 7517f99 to preserve the input's UTC offset when both occurrences of a repeated DST fall-back hour match the target local hour, and added test cases for both occurrences (CEST and CET). |
nishantmonu51
left a comment
There was a problem hiding this comment.
One more DST case inline.
| targetHour := t.Hour() - n | ||
| // Build the boundary from local clock fields: time.Truncate aligns to UTC | ||
| // hour boundaries, which are not on the hour in zones with a fractional offset. | ||
| res := time.Date(t.Year(), t.Month(), t.Day(), targetHour, 0, 0, 0, t.Location()) |
There was a problem hiding this comment.
PT1H can still return a boundary after the watermark when the clock jumps across the middle of an hour. I checked 2024-09-28T14:05:00Z in Pacific/Chatham: the input is 03:50, 03:00 is in the spring-forward gap, and time.Date returns 04:00. calculateReportExecutionTimes and calculateAlertExecutionTimes can treat that future boundary as closed. Could we handle nonexistent wall times explicitly and use the first instant after the gap (03:45 here)? Please add this case alongside the Copenhagen fold tests.
Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com>
nishantmonu51
left a comment
There was a problem hiding this comment.
One way to shorten the gap handling inline.
| // before target, i.e. the first instant after a DST gap that contains target. | ||
| // res must be an instant at or after that point (time.Date's normalization of a | ||
| // nonexistent wall time satisfies this). | ||
| func firstInstantAfterGap(res time.Time, loc *time.Location, target time.Time) time.Time { |
There was a problem hiding this comment.
Could we use res.ZoneBounds() here? When the normalized wall clock is before the target, the current zone’s end is the transition we want. When it’s after the target, the current zone’s start is the transition. time.Date can land on either side, as New York and Chatham do respectively, and this uses the transition directly without the week offsets or the binary search.
nishantmonu51
left a comment
There was a problem hiding this comment.
One small cleanup inline.
| // The requested wall time does not exist (it falls inside a | ||
| // spring-forward gap), so time.Date normalized it to a later or | ||
| // earlier instant. Use the transition instant after the gap instead. | ||
| res = firstInstantAfterGap(res, wallTimeKey(time.Date(t.Year(), t.Month(), t.Day(), targetHour, 0, 0, 0, time.UTC))) |
There was a problem hiding this comment.
Could we pass the UTC time.Date directly? wallTimeKey extracts the fields and rebuilds them in UTC, which leaves this value exactly as it started.
| res = firstInstantAfterGap(res, wallTimeKey(time.Date(t.Year(), t.Month(), t.Day(), targetHour, 0, 0, 0, time.UTC))) | |
| res = firstInstantAfterGap(res, time.Date(t.Year(), t.Month(), t.Day(), targetHour, 0, 0, 0, time.UTC)) |
Summary
Fix
StandardDuration.Truncateso that hour-based truncation aligns to local clock hour boundaries instead of UTC hour boundaries. Adds regression coverage for time zones whose UTC offset is not a whole number of hours.Root Cause & Problem
The hour branch of
StandardDuration.Truncateusedt.Truncate(time.Hour). Go'stime.Time.Truncateoperates on the absolute instant since the zero time, so it snaps to UTC hour boundaries. In a zone with a fractional UTC offset, those boundaries do not fall on local whole hours.For example, in Asia/Kolkata (UTC+05:30), truncating
2022-07-15 10:45 +0530produced10:30 +0530, and in Asia/Kathmandu (UTC+05:45), truncating10:15 +0545produced09:45 +0545(even the wrong local hour). Anything using this method to bucket timestamps into hour grains, such as time-series dashboards, gets boundaries that are offset by the zone's fractional offset.Technical Solution
Build the boundary from the timestamp's local clock fields with
time.Date(t.Year(), t.Month(), t.Day(), t.Hour()-n, 0, 0, 0, t.Location()). This keeps the original location and preserves the existing DST behavior: nonexistent local times (spring forward) normalize forward just asTruncatedid, and ambiguous local times (fall back) resolve to the same offset as the input. Existing UTC and DST test cases remain unchanged.Test Plan
Commands run from the repository root with Go 1.27.3:
D:\tools\go\bin\go.exe test ./runtime/pkg/duration/ -run TestTruncate -count=1before the fix: FAIL. The two new cases produced10:30 +0530and09:45 +0545where10:00was expected.D:\tools\go\bin\go.exe vet ./runtime/pkg/duration/: no findings.D:\tools\go\bin\go.exe test ./runtime/pkg/duration/ -count=1after the fix:ok github.com/rilldata/rill/runtime/pkg/duration 0.444s, all cases pass including the existing UTC, spring DST, and fall DST cases.gofmt -lon LF-normalized copies of both changed files reported no differences (the checkout usescore.autocrlf=true, which makes a directgofmt -lreport every CRLF file on Windows).No full repository test suite was run locally; CI covers the wider suite.