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: add DateTime, DateOnly, TimeOnly format constants #52746

Closed
dsnet opened this issue May 6, 2022 · 22 comments
Closed

time: add DateTime, DateOnly, TimeOnly format constants #52746

dsnet opened this issue May 6, 2022 · 22 comments

Comments

@dsnet
Copy link
Member

dsnet commented May 6, 2022

I propose the declaration of the following format constants to the time package:

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

Analyzing all Go code on the module proxy (as of 2022-04-01), here's a table of all common time formats sorted in popularity order:

Rank Frequency Format
1 75616 time.RFC3339
2 23954 time.RFC3339Nano
3 13312 "2006-01-02 15:04:05"
4 12332 "2006-01-02"
5 11940 time.RFC1123
6 2998 time.RFC1123Z
7 2871 time.UnixDate
8 2843 time.ANSIC
9 2820 "20060102"
10 2600 time.RFC822
11 2522 time.Kitchen
12 2499 "20060102150405"
13 2225 "15:04:05"
14 1503 time.RFC850
15 1410 time.Stamp
16 1374 "2006-01-02T15:04:05Z"
17 1302 "2006-01-02T15:04:05"
18 1152 "2006"
19 1149 "2006/01/02 15:04:05"
20 1133 time.StampMilli
21 1122 time.RFC822Z
22 1059 time.RubyDate
23 962 "15:04"
24 934 "2006-01-02 15:04:05.000"
25 879 "2006010215"
26 685 "15:04:05.000"
27 668 "2006-01-02 15:04"
28 646 "2006/01/02 - 15:04:05"
29 637 "2006/01/02"
30 552 time.StampNano
31 533 time.StampMicro

As can be seen, "2006-01-02 15:04:05" and "2006-01-02" are the 3rd and 4th most popular format constants. They are more popular than most of the other time format constants that did get a named declaration.

Given their popularity, I propose declaring:

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

Other constant name suggestions welcome.

@dsnet dsnet added the Proposal label May 6, 2022
@gopherbot gopherbot added this to the Proposal milestone May 6, 2022
@meling
Copy link

meling commented May 9, 2022

Perhaps TimeOnly for "15:04:05" at 13th place would also make sense.

@mpx
Copy link
Contributor

mpx commented May 10, 2022

ISODate: "2006-01-02" Is the ISO 8601 extended date format (basic is 20060102).
ISOTime: "15:04:05" is the ISO 8601 extended time format (basic is 150405).

RFC3339 provides the extended ISO date and time format - using T as the separator (mandatory) and including the timezone.

Perhaps something like ISODateAndTime could be used? Technically, ISODateTime should be RFC3339 without the timezone since ISO requires using the T (hence "DateAndTime").

@rsc
Copy link
Contributor

rsc commented May 11, 2022

It sounds like maybe

const (
    ISO8601 = "2006-01-02 15:04:05"
    ISO8601Nano = "2006-01-02 15:04:05.999999999"
    ISO8601Date = "2006-01-02"
)

This would align with RFC3339 and RFC3339Nano and add the date-only form.

Thoughts?

/cc @robpike

@rsc
Copy link
Contributor

rsc commented May 11, 2022

Also, thanks for the great data @dsnet

@robpike
Copy link
Contributor

robpike commented May 11, 2022

The ISO names are fine, but I see advantages to also having the nicer names like TimeOnly and DateOnly as aliases. I remain a fan of time.Kitchen, which is underused.

@dsnet
Copy link
Member Author

dsnet commented May 13, 2022

I'm used to reading specs, but I'm overwhelmed by ISO 8601 🤯. On Wikipedia, it says:

Separating date and time parts with other characters such as space is not allowed in ISO 8601, but allowed in its profile RFC 3339.

If true, it seems that ISO8601 = "2006-01-02 15:04:05" is not be valid ISO 8601. What's really odd is that the source of that statement comes from RFC 3339 with no reference to where in ISO 8601 it says that.

(Side comment: Thank you Go language for having a simple and concise language specification).

@rsc
Copy link
Contributor

rsc commented May 18, 2022

OK, it sounds like we can't call these ISO8601, and maybe that's not specific enough either. But what should we call them instead?

    DateTime = "2006-01-02 15:04:05"
    DateTimeNano = "2006-01-02 15:04:05.999999999"
    DateOnly = "2006-01-02"
    TimeOnly = "15:04:05"
    TimeOnlyNano = "15:04:05.999999999"

But also the 9999 comes from the RFC3339 definition; Stamp has 0s instead, meaning that the digits are always there:

	Stamp      = "Jan _2 15:04:05"
	StampMilli = "Jan _2 15:04:05.000"
	StampMicro = "Jan _2 15:04:05.000000"
	StampNano  = "Jan _2 15:04:05.000000000"

I don't know which of these we'd want either.

Maybe we are biting off too much and should just do:

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

and stop there?

@dsnet
Copy link
Member Author

dsnet commented May 19, 2022

DateTime and DateOnly SGTM. I'm also okay with throwing in TimeOnly in the mix per #52746 (comment).

The Nano variants seem a bit excessive and don't have data to support their utility.

@rsc
Copy link
Contributor

rsc commented May 25, 2022

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@AlexanderYastrebov
Copy link
Contributor

The proposed formats are defined in the Internet Date/Time Format https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 so maybe names could be either RFC3339DateTime, RFC3339Date and RFC3339Time or InternetDateTime InternetDate and InternetTime.

@fzipp
Copy link
Contributor

fzipp commented May 28, 2022

names could be either RFC3339DateTime, RFC3339Date and RFC3339Time

My preference is for a human-friendly name without yet another number sequence.

@AlexanderYastrebov
Copy link
Contributor

My preference is for a human-friendly name without yet another number sequence.

What about InternetDateTime, InternetDate and InternetTime then to get away without lame Only suffix?

@meling
Copy link

meling commented May 28, 2022

My preference is for a human-friendly name without yet another number sequence.

What about InternetDateTime, InternetDate and InternetTime then to get away without lame Only suffix?

Only has an obvious meaning. The Internet prefix has nothing to do with these time formats.

@rsc
Copy link
Contributor

rsc commented Jun 1, 2022

Given @dsnet's data, let's consider doing:

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

Does anyone object to these?

@AlexanderYastrebov
Copy link
Contributor

Analyzing all Go code on the module proxy (as of 2022-04-01), here's a table of all common time formats sorted in popularity order:

Would it be possible to tell how users name these constants?

names could be either RFC3339DateTime, RFC3339Date and RFC3339Time

My preference is for a human-friendly name without yet another number sequence.

Looking at the top two popular formats I think users mean to use "reduced" RFC3339 format. It is not something else because only RFC3339 format puts fields in descending order making string representations sortable. The field separator is also the same -.

The Internet prefix has nothing to do with these time formats.

As per #52746 (comment) these formats are somewhat defined in the RFC section named "Internet Date/Time Format". It also has a note about space separator used in these formats:

NOTE: ISO 8601 defines date and time separated by "T".
Applications using this syntax may choose, for the sake of
readability, to specify a full-date and full-time separated by
(say) a space character.

Usage in situ:

time.Parse(time.DateTime, s)
time.Parse(time.DateOnly, s)
time.Parse(time.TimeOnly, s)

vs

time.Parse(time.RFC3339DateTime, s)
time.Parse(time.RFC3339Date, s)
time.Parse(time.RFC3339Time, s)

vs

time.Parse(time.InternetDateTime, s)
time.Parse(time.InternetDate, s)
time.Parse(time.InternetTime, s)

@rsc
Copy link
Contributor

rsc commented Jun 8, 2022

Would it be possible to tell how users name these constants?

I think these were taken directly from the call sites. They name them like "2006-01-02".

@rsc rsc changed the title proposal: time: add format constants for date and date+time proposal: time: add DateTime, DateOnly, TimeOnly format constants Jun 8, 2022
@rsc
Copy link
Contributor

rsc commented Jun 8, 2022

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jun 15, 2022

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: time: add DateTime, DateOnly, TimeOnly format constants time: add DateTime, DateOnly, TimeOnly format constants Jun 15, 2022
@rsc rsc removed this from the Proposal milestone Jun 15, 2022
@rsc rsc added this to the Backlog milestone Jun 15, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/412495 mentions this issue: time: add DateTime, DateOnly, and TimeOnly

@dmitshur dmitshur modified the milestones: Backlog, Go1.20 Aug 9, 2022
jproberts pushed a commit to jproberts/go that referenced this issue Aug 10, 2022
Add named constants for the 3rd, 4th, and 13th most popular formats.

Fixes golang#52746

Change-Id: I7ce92e44dcae18c089124f1d6f5bc2d6359d436c
Reviewed-on: https://go-review.googlesource.com/c/go/+/412495
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Joseph Tsai <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Joseph Tsai <[email protected]>
@rsc rsc moved this to Accepted in Proposals Aug 10, 2022
@rsc rsc added this to Proposals Aug 10, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/433277 mentions this issue: cmd/go/internal: optimize code with newly defined time.DateTime const…

gopherbot pushed a commit that referenced this issue Sep 27, 2022
Use the newly defined time.Datetime constant instead of a string literal.

Updates #52746

Change-Id: I2722415ecc67fd2adfdab2eaba3298774032bff3
GitHub-Last-Rev: 65d3aa9
GitHub-Pull-Request: #55833
Reviewed-on: https://go-review.googlesource.com/c/go/+/433277
Run-TryBot: Joseph Tsai <[email protected]>
Auto-Submit: Joseph Tsai <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Joseph Tsai <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
@gouguoyin

This comment was marked as off-topic.

@mvdan
Copy link
Member

mvdan commented Dec 16, 2022

@gouguoyin this proposal is accepted and implemented, and you don't seem to be proposing anything for the time package, so that seems off-topic to me.

@rsc rsc removed this from Proposals Aug 9, 2023
isaaclu0897 added a commit to isaaclu0897/HermInvest that referenced this issue Dec 11, 2023
* Upgrade Go version from 1.19 to 1.20
* Utilize 'time.Dateonly' for enhanced formatting, see:
golang/go#52746
isaaclu0897 added a commit to isaaclu0897/HermInvest that referenced this issue Dec 11, 2023
* Upgrade Go version from 1.19 to 1.20
* Utilize 'time.Dateonly' for enhanced formatting, see: golang/go#52746
@golang golang locked and limited conversation to collaborators Dec 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests