-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time: better handling of overflow in time.Time #71979
Comments
Related Issues
Related Code Changes
Related Documentation (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
In #63844 it was noted that restricting to ±32767 years wasn't really acceptable |
If it isn't clear, this issue isn't about changing the range of supported instants. It's about what we do when some time package operation goes outside that range. |
Personally, I would expect to overflow to use saturation arithmetic. It's what I've observed for Do you have any suggestions for |
Maybe there should be a saturating |
The
time.Time
type has a large, but finite range. The time package does not, so far as I can determine, document what this range is.A
Time
can represent instants billions of years in the past or future. So far as I can tell, we've taken a general attitude that instants outside the probable lifespan of the human species are out of scope for the package and can be ignored.For example,
time.Unix
converts a Unix timestamp (seconds & nanoseconds since the Unix epoch) to aTime
. It documents the handling of out-of-range values only by saying:Functions such as
time.AddDate
do not document their behavior on overflow.Most programs will never encounter out-of-range dates. (If Go proves to be unexpectedly durable on geologic time scales, we can leave the problem of increasing the size of the
time.Time
seconds counter to future generations.) However, a program handling adversarial inputs (such as a very large Unix timestamp) may be induced to overflow atime.Time
, with surprising and undocumented results.We could harden the time package a bit more against overflow. Some specific functions that we might harden:
time.Unix
could either saturate or return the zero time when provided with an out-of-range value.time.Add
andtime.AddDate
could saturate rather than wrapping on overflow.time.Round
could either truncate or saturate on overflow.There might be others I'm missing.
Alternatively, we could leave the implementation alone and document what validation users should perform to adequately defend against adversarial inputs to
time.Unix
.The text was updated successfully, but these errors were encountered: