-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: time: add "1136214245" as layout string for unix timestamp #65665
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
Comments
That strings seems pretty hard to remember. And it's already possible to call |
TBH, after over 10 years of writing Go, I never remember 2006-01-02T15:04:05Z07:00 either, and usually have to refer to the |
This is a very odd request, asking a string parser to accept a purely numeric value. If you have the number, you don't need to parse it again. Also, you should be using ParseUint not ParseFloat. |
The source data gets coalesced into a string because all of the possible source types implement
Edit to add: I would love if all of my input was strongly typed, but for interoperability with JSON or YAML or CLI flags or other dynamic languages and configuration formats, you sometimes have to be liberal in what you accept. It is not uncommon to see a JSON file edited by a human which ends up with something like For another example, consider |
Based on the discussion above, this proposal seems like a likely decline. |
It would be very useful to have some way to get a UNIX time stamp out of |
I'll also note, that having a format-specifier doesn't just allow to parse a string consisting entirely of a number as a time - it also allows to use that format-specifier in a substring. For example, a git commit internally stores author/commiter information as e.g.
This proposal would allow a parser of this format to split off the name/email part and then call time.Parse("1136214245 -0700", "1693893277 +0200") to parse the time in the appropriate time zone. I actually ran into this many years ago, when trying to implement a puro-go git library. I'm still not sure it's worth including, though. Also, I'll note that I disagree that this can be implemented in a fully backwards compatible manner: time.Parse("1136214245", "1136214245") Currently this returns an error "month out of range", while under this proposal, it would presumably return the reference time. In particular, it seems the layout-parser currently interprets the first "1" as the numeric month format specifier. |
No change in consensus, so declined. |
Proposal Details
2006-01-02T15:04:05Z07:00 as a UNIX timestamp (seconds since January 1, 1970) is 1136214245. (In milliseconds, it's 1136214245000; in microseconds, it's 1136214245000000; in nanoseconds, it's 1136214245000000000.)
I propose being able to use these values as layouts with
time.Parse
. For instance, the result oftime.Parse("1136214245", "1707688329")
would be Sun, 11 Feb 2024 21:40:11 GMT. These values are unambiguous with the current std string and I believe could be added in a fully reverse-compatible manner.My motivation is that I have some library code which converts from
fmt.Stringer
(such as a field from JSON input data) totime.Time
by guessing several common layouts. It currently special-cases situations wherestrconv.ParseFloat
succeeds, and then tries to guess the appropriate unit. This could be simplified if the standard library supported these layouts.The text was updated successfully, but these errors were encountered: