Skip to content

time: Parse formats allow ambiguity #67750

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

Closed
m-messiah opened this issue May 31, 2024 · 3 comments
Closed

time: Parse formats allow ambiguity #67750

m-messiah opened this issue May 31, 2024 · 3 comments

Comments

@m-messiah
Copy link

Go version

go version go1.22.3 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOOS='darwin'

What did you do?

Time Parse always assumes RFC3339 date and does not allow to change it.

For example https://go.dev/play/p/1gWqncWgd0_h:

a, err := time.Parse("2006-21-01", "2024-23-03")

expected to parse the date as 23 March 2024, but fails with error parsing time "2024-23-03" as "2006-21-01": cannot parse "-03" as "1"

Moreover, time.DateOnly format (as well as time.DateTime) assume RFC3339 with ambiguity to anyone who does not follow the RFC, as both 01 and 02 could be treated as Month

go/src/time/format.go

Lines 123 to 124 in 5bf8c0c

DateTime = "2006-01-02 15:04:05"
DateOnly = "2006-01-02"

What did you see happen?

https://go.dev/play/p/qnIFX3Q_zUL

1 January 1 parsing time "2024-23-03" as "2006-21-01": cannot parse "-03" as "1"
2006-01-02

What did you expect to see?

Expect time.Parse to follow the format provided without hidden assumptions leading to errors.
Expect time.DateOnly and time.DateTime to provide self-explanatory format without ambiguity.

@seankhliao
Copy link
Member

That's the wrong layout to use. time.Parse correctly treated it as:

  • 2006: 2004
  • -: -
  • 2: 23
  • 1: -03, an error

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@m-messiah
Copy link
Author

Ok, I reread the Layout comment and it has some handy things about Day and Month:

go/src/time/format.go

Lines 60 to 62 in 5bf8c0c

// Month: "Jan" "January" "01" "1"
// Day of the week: "Mon" "Monday"
// Day of the month: "2" "_2" "02"

saying month is always 01 and day is always 02, meaning my playground could be rewritten as https://go.dev/play/p/CcPYcJ5rRPN

And it works, correctly returning the 23 of March, but go vet sends warning about such format after #48801.

@rsc
Copy link
Contributor

rsc commented Jun 1, 2024

Where did you encounter yyyy-dd-mm dates? When we accepted #48801, we believed they were exceedingly rare.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants