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: date fields have not the same value at parsing a time layout #29902

Closed
tredoe opened this issue Jan 23, 2019 · 7 comments
Closed

time: date fields have not the same value at parsing a time layout #29902

tredoe opened this issue Jan 23, 2019 · 7 comments

Comments

@tredoe
Copy link

@tredoe tredoe commented Jan 23, 2019

When it is parsed a time, then the date fields are not set with the same value.

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

$ go version
go version go1.11.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

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

What did you expect to see?

I expected that the the fields of the date were all to zero (0000-00-00) or at least all with the same value (0001-01-01)

What did you see instead?

The date is set to: 0000-01-01

@vearutop
Copy link
Contributor

@vearutop vearutop commented Jan 23, 2019

0000-01-01 is a valid date: January 1 0000, while 0000-00-00 is not (neither zero month, nor zero day are in calendar).

@tredoe
Copy link
Author

@tredoe tredoe commented Jan 24, 2019

But I'm not expecting a valid value for the date since the value to be used is going to be the time; I was expecting that only the data to be used (the time at this case) was set.

@vearutop
Copy link
Contributor

@vearutop vearutop commented Jan 24, 2019

https://godoc.org/time#Parse

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. 
@FiloSottile
Copy link
Member

@FiloSottile FiloSottile commented Jan 24, 2019

time.Parse returns a time.Time object, which represents a moment in time. There is no split between date and time, so it can't return the one and not the other. 0000-00-00 does not exist so there is no way to represent it as a time.Time.

This is working as expected and as documented (#29902 (comment)).

@tredoe
Copy link
Author

@tredoe tredoe commented Jan 24, 2019

But since the zero value (at date) of time.Time{} is set to "0001-01-01", then I think that such value also should be used instead of (0000-01-01).

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

@FiloSottile
Copy link
Member

@FiloSottile FiloSottile commented Jan 24, 2019

I don't necessarily disagree, but since this behavior is documented it falls under the Go 1 Compatibility Promise and can't be changed.

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.

@tredoe
Copy link
Author

@tredoe tredoe commented Feb 5, 2019

Added a proposal for Go 2: #30086

@golang golang locked and limited conversation to collaborators Feb 5, 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
4 participants
You can’t perform that action at this time.