Skip to content

Reject encoding time.Time when Go formats wrong year - #838

Merged
fxamacker merged 1 commit into
release-2.9from
fxamacker/release-2.9-fix-encoding-rfc3339-time-wrong-year
Oct 2, 2026
Merged

fxamacker merged 1 commit into
release-2.9from
fxamacker/release-2.9-fix-encoding-rfc3339-time-wrong-year

Conversation

@fxamacker

@fxamacker fxamacker commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Not affected: user apps that encode time.Time with TimeUnix (the default), TimeUnixMicro, or TimeUnixDynamic, or that don't encode times about 292 billion years before 1970.

Go's standard library doesn't fully support time values in a ~257-year range about 292 billion years ago. Some Go standard library functions still work but some will return wrong results for time.Time inside that range.

For the unsupported ~257-year range about 292 billion years ago, t.Unix() still works while some Go functions like t.Year() or t.Format() can return a future year instead of a past year. For example, Go formats time.Unix(math.MinInt64, 0).UTC() as "292277026596-12-04T15:30:08Z".

Previously, encoding CBOR time as a text string (instead of integer or float) didn't check if Go returned an incorrect string for time.Time values in the unsupported ~257-year range over 292 billion years ago.

This PR returns UnsupportedValueError when the formatted year returned by the Go standard library is wrong. This doesn't change encoding of any other time value with a time zone offset up to 168 hours (one week).

For perspective, this only affects dates about 292 billion years ago and the estimated age of the universe is less than 14 billion years.

Not affected: user apps that encode time.Time with TimeUnix (the
default), TimeUnixMicro, or TimeUnixDynamic, or that don't encode times
about 292 billion years before 1970.

Go's standard library doesn't fully support time values in a ~257-year
range about 292 billion years before 1970.  Some Go standard library
functions still work but some will return wrong results for time.Time
inside that range.

Go's time formatting writes a wrong date for a local time before the
earliest time Go supports: the year is about 292 billion years after
1970 instead of before.  For example, Go formats
time.Unix(math.MinInt64, 0).UTC() as "292277026596-12-04T15:30:08Z".

Previously, encoding CBOR time as a text string (instead of integer or
float) didn't check if Go returned an incorrect string for time.Time
values in the unsupported ~257-year range over 292 billion years ago.

This commit returns UnsupportedValueError when the formatted year
returned by the Go standard library is wrong.  This commit doesn't
change encoding of any other time value with a time zone offset up to
168 hours (one week).

For perspective, this only affects dates about 292 billion years ago and
the estimated age of the universe is less than 14 billion years.
@fxamacker fxamacker self-assigned this Oct 1, 2026
@fxamacker
fxamacker changed the base branch from fxamacker/release-2.9-fix-encoding-timeunixmicro to release-2.9 October 1, 2026 22:40
@fxamacker
fxamacker merged commit ac3c8da into release-2.9 Oct 2, 2026
25 checks passed
fxamacker added a commit that referenced this pull request Oct 2, 2026
@fxamacker
fxamacker deleted the fxamacker/release-2.9-fix-encoding-rfc3339-time-wrong-year branch October 27, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants