Skip to content
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: Parse(..) should use year 1 when no year is given to match the time.Time zero value #29840

Closed
fsmv opened this issue Jan 20, 2019 · 3 comments
Closed

Comments

@fsmv
Copy link

@fsmv fsmv commented Jan 20, 2019

What version of Go are you using (go version)?

$ go version
go version go1.10.3 linux/arm

What did you do?

https://play.golang.org/p/_WyGwuxEAhL

What did you expect to see?

time.Parse(..) should set year to 1, like the zero value of time.Time, when year is omitted from the layout string.

What did you see instead?

time.Parse(..) sets the year to 0, so you can't compare with the zero value of time.Time, when the year is omitted from the layout string.

@ianlancetaylor ianlancetaylor changed the title time.Parse(..) should use year 1 when no year is given to match the time.Time zero value time: Parse(..) should use year 1 when no year is given to match the time.Time zero value Jan 22, 2019
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 22, 2019

As far as I can tell, the only reason to do this would be so that you can compute offsets from a zero time value. I suggest that you simply don't do that. If you want to compare the result of time.Parse without a year, then compare it to the result of time.Parse of some appropriate constant string.

The current behavior is documented and has been documented since Go 1.1, so we would need a really really good reason to change it now.

Elements omitted from the value are assumed to be zero or, when zero is impossible, one, so parsing "3:04pm" returns the time corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is 0, this time is before the zero Time). Years must be in the range 0000..9999. The day of the week is checked for syntax but it is otherwise ignored.

@fsmv
Copy link
Author

@fsmv fsmv commented Jan 22, 2019

Definitely it's a minor thing. It just seemed like an inconsistency between the time.Time zero value and the zero value time.Parse uses. Do you happen to know if there's a good reason for using a different default year in time.Parse when all other fields get filled with the zero value?

I thought maybe a small fix change like this could happen as a part of Go 2. I understand though that it's still not easy to change the standard library behavior.

Thanks for the reply!

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 22, 2019

time.,Parse does use year 0, so the question is why the zero value of time.Time uses year 1. That was chosen to avoid having a negative year for the zero value in time zones west of UTC.

@golang golang locked and limited conversation to collaborators Jan 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.