Skip to content

fix(os/gtime): fix Timestamp overflow outside the UnixNano range - #4989

Merged
hailaz merged 3 commits into
gogf:masterfrom
ruziba3vich:fix-gtime-timestamp-overflow
Nov 25, 2026
Merged

hailaz merged 3 commits into
gogf:masterfrom
ruziba3vich:fix-gtime-timestamp-overflow

Conversation

@ruziba3vich

Copy link
Copy Markdown
Contributor

Timestamp, TimestampMilli and TimestampMicro divide UnixNano(). UnixNano is only defined for the years 1678 to 2262, so outside that range the result is wrong:

t := gtime.NewFromTime(time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC))
t.Timestamp() // -4852116232, want 253402300799

1600-01-01 UTC gives 6770648073 instead of -11676096000. Dates like 9999-12-31 are common as "no end date" values. TimestampStr, TimestampMilliStr and TimestampMicroStr return the same wrong numbers.

The fix uses Unix(), UnixMilli() and UnixMicro() of the standard library, which do not overflow there.

One behaviour difference: for a time before 1970 with a fractional second the result is now floored, as time.Time.Unix does (1969-12-31 23:59:59.5 gives -1, it was 0).

Test: Test_Timestamp_OutOfUnixNanoRange fails on master with EXPECT -4852116232 == 253402300799. I ran go test -race ./os/gtime/ with TZ=Asia/Shanghai.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.10%. Comparing base (18c9f26) to head (faa64a4).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4989      +/-   ##
==========================================
- Coverage   85.11%   85.10%   -0.02%     
==========================================
  Files         893      893              
  Lines       47141    47141              
==========================================
- Hits        40125    40120       -5     
- Misses       7016     7021       +5     
Flag Coverage Δ
go-1.27-core 72.85% <ø> (-0.01%) ⬇️
go-1.27-drivers-1 27.55% <ø> (ø)
go-1.27-drivers-2 27.37% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gqcn

gqcn commented Nov 23, 2026

Copy link
Copy Markdown
Member

Timestamp, TimestampMilli, and TimestampMicro look right. Dividing UnixNano() is undefined outside years 1678–2262, and Unix(), UnixMilli(), and UnixMicro() are the standard-library equivalents. The new expectations for 9999-12-31 and 1600-01-01 match those methods. The pre-1970 change (1969-12-31 23:59:59.5 from 0 to -1) is the same floor time.Time.Unix already uses; whole seconds before 1970 stay the same.

codecov/project is still red on this head, so this is not ready to merge and I have not added bot-approved. It reports project coverage 85.05% (-0.01%) against 8f0e07d: https://app.codecov.io/gh/gogf/gf/pull/4989. codecov/patch is 100%, the Go test jobs passed, and the line count did not change (4 fewer hits outside this diff). That looks like coverage noise rather than a gap in these two files. Re-running the coverage check should be enough; no extra coverage workaround is needed here.

@ruziba3vich
ruziba3vich force-pushed the fix-gtime-timestamp-overflow branch from 6364afe to c6d46ab Compare November 23, 2026 15:34
@gqcn

gqcn commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks for updating the branch. The new Test_Timestamp_OutOfUnixNanoRange cases in os/gtime/gtime_z_unit_time_test.go look solid and add helpful coverage for out-of-range dates beyond what #5005 already tests, though a couple of things could use attention before merging:

  • Suggested priority CI (codecov/project): the check on the current head is still failing and needs to pass before merge. The reported error is 85.10% (-0.02%) compared to 18c9f26 (https://app.codecov.io/gh/gogf/gf/pull/4989). Since codecov/patch and all Go test jobs passed and this diff only touches a test file with the same total line count (47141, 5 fewer hits elsewhere), this looks like coverage fluctuation rather than a gap in this PR. Consider re-running the CI coverage check instead of adding any coverage workaround.
  • Suggested improvement PR title and description: since the os/gtime/gtime_time.go fix was already merged into master via fix(os/gtime,os/gcron,internal/tracing): use the Unix time helpers #5005, this PR's current diff only adds unit tests in os/gtime/gtime_z_unit_time_test.go. Keeping the fix(os/gtime): title and original description could be misleading in the commit history once merged. Consider changing the title to use test(os/gtime): (for example, test(os/gtime): add Timestamp tests outside the UnixNano range) and updating the description to reflect the test-only scope.

I have not added the bot-approved label yet.

@hailaz
hailaz merged commit 5fbc462 into gogf:master Nov 25, 2026
16 of 17 checks passed
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.

3 participants