Skip to content

fix(duration): truncate hours on local boundaries in fractional-offset zones - #10187

Open
Tyagiquamar wants to merge 4 commits into
rilldata:mainfrom
Tyagiquamar:fix/duration-truncate-local-hour
Open

Tyagiquamar wants to merge 4 commits into
rilldata:mainfrom
Tyagiquamar:fix/duration-truncate-local-hour

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

Fix StandardDuration.Truncate so 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.Truncate used t.Truncate(time.Hour). Go's time.Time.Truncate operates 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 +0530 produced 10:30 +0530, and in Asia/Kathmandu (UTC+05:45), truncating 10:15 +0545 produced 09: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 as Truncate did, 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=1 before the fix: FAIL. The two new cases produced 10:30 +0530 and 09:45 +0545 where 10:00 was expected.
  • D:\tools\go\bin\go.exe vet ./runtime/pkg/duration/: no findings.
  • D:\tools\go\bin\go.exe test ./runtime/pkg/duration/ -count=1 after 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.
  • Formatting: gofmt -l on LF-normalized copies of both changed files reported no differences (the checkout uses core.autocrlf=true, which makes a direct gofmt -l report every CRLF file on Windows).

No full repository test suite was run locally; CI covers the wider suite.

@nishantmonu51
nishantmonu51 self-requested a review November 19, 2026 11:57

@nishantmonu51 nishantmonu51 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One DST case inline.

Comment thread runtime/pkg/duration/duration.go Outdated
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())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Tyagiquamar

Copy link
Copy Markdown
Contributor Author

@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 nishantmonu51 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 nishantmonu51 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to shorten the gap handling inline.

Comment thread runtime/pkg/duration/duration.go Outdated
// 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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 nishantmonu51 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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))

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants