Skip to content

time: Parse formats allow ambiguity #67750

@m-messiah

Description

@m-messiah

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions