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

proposal: time.Parse: support letter-based formats, such as "yyyy-mm-dd HH:MM:SS" #66364

Closed
TheCoreMan opened this issue Mar 17, 2024 · 4 comments
Labels
Milestone

Comments

@TheCoreMan
Copy link

TheCoreMan commented Mar 17, 2024

Proposal Details

Previous work and context

This is a continuation of two previous discussions (there might be more, but I haven't found them):

Comparison to other languages

I propose we support a layout that's much easier to read and understand, similar to other time format options. Existing references include:

All these examples are widely used and, very notably, don't work the same way as we do it in Go.

What do I think we should do in Go, then?

I suggest two options to consider:

Keep the current API and add support for layouts with chars

Which will look like this (I randomly picked the Python letters/format, but we could pick another layout table if we want).

    tm, err := time.Parse("%d/%m/%y %H:%M:%S.%f", "31/01/22 23:59:59.999999") 

Add a "Layout" type to the time.Parse API, similar to how Rust does it

This is a bit cumbersome - but honestly, I've never been able to use time.Parse without having to double-check which parameter is the layout and which parameter is the input. So something like:

myCoolLayout, layoutErr := time.NewLayout("%d/%m/%y %H:%M:%S.%f") // Error if format is bad, e.g. includes unknown letters?
if layoutErr != nil {
    // Handle it however you want, I assume this is almost always a panic
}
myTime, timeErr := time.Parse(myCoolLayout, ""31/01/22 23:59:59.999999"")
@gopherbot gopherbot added this to the Proposal milestone Mar 17, 2024
@seankhliao
Copy link
Member

Duplicate of #40287

@seankhliao seankhliao marked this as a duplicate of #40287 Mar 17, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2024
@shayn-orca
Copy link

shayn-orca commented Mar 17, 2024

@seankhliao can the long list of people who already suggested this, and the feedback, warrant a re opening of this issue?

Bare minimum, if the x-in-std lib argument is a winner, can we update the docs for time.Parse to let people know about the strftime stuff that lives outside the stdlib?

Edit: oops, answered from my Corp account, but I'm the OP 😅

@TheCoreMan
Copy link
Author

Not only that, but looking for non-stdlib-implementations, looks like there are many, and non are "dominating". So the same case that was made for keeping YAML out of the STDLIB (see #61023) by @rsc:

On top of that, @niemeyer has done an excellent job writing gopkg.in/yaml.v3, which as the top comment points out is the most widely used YAML package in the Go ecosystem. For that I am incredibly grateful, and I think we should continue to encourage use of that package.

Can't be made here. See: https://pkg.go.dev/search?q=strftime

@seankhliao
Copy link
Member

To allow us to make progress, once we've made a decision, we will generally not relitigate without significant new information.

While popularity is a factor to consider, the general lack of popularity of third party packages (relative to recent additions such as http mux patterns or structured logging or non-additions such as yaml) would indicate that it isn't there.

Go generally aims to have a single way to do things, at least within the standard library, and we have one that works for time. While this doesn't preclude the existence of third party packages like strftime, it is a reason for it not to refer to arbitrary outside resources.

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

No branches or pull requests

4 participants