Repository navigation
fix: encode UNIXMS/UNIXMICRO times outside the UnixNano range - #826
Open
NgoQuocViet2001 wants to merge 1 commit into
Open
NgoQuocViet2001 wants to merge 1 commit into
NgoQuocViet2001 wants to merge 1 commit into
Conversation
The JSON encoder derived UNIXMS and UNIXMICRO values from t.UnixNano(), which overflows int64 for times before 1678 or after 2262. The zero time.Time and far-future sentinels such as 9999-12-31 were logged as unrelated dates, while TimeFormatUnix printed them correctly. Use t.UnixMilli and t.UnixMicro in AppendTime, and scale seconds and nanoseconds separately in AppendTimes. Pre-1970 times with a sub-unit fraction now round down like TimeFormatUnix and time.Time.UnixMilli instead of toward zero. UNIXNANO output is unchanged.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
TimeFieldFormatset toTimeFormatUnixMsorTimeFormatUnixMicro, the JSON encoder computed the value ast.UnixNano()/div.UnixNanooverflows for times before 1678 or after 2262, so the zerotime.Timeand far-future sentinels are logged as unrelated dates, whileTimeFormatUnixprints the same values correctly:AppendTimenow usest.UnixMilli()andt.UnixMicro(), andAppendTimesscales seconds and nanoseconds separately in the same way. UNIXNANO output is unchanged. One visible side effect: pre-1970 times with a sub-unit fraction now round down likeTimeFormatUnixandUnixMilli(time.Unix(-1, 999999000)gives -1 ms instead of 0). I can drop that test case if you prefer a narrower change. #813 fixes the parsing side in ConsoleWriter and is independent of this one.go test ./...,go test -tags binary_log ./...,go vet ./...andgofmt -lon the changed files: all clean.TestAppendTimeUnixOutsideNanoRangefails on master with 10 assertion failures and passes with the fix.AppendTimesmatchesUnixMilli,UnixMicroandUnixNanofor 200k random times between years 1 and 9999, and a 10-value UNIXMS benchmark shows no slowdown.