-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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 Time.UnixMilli and Time.UnixMicro (like Time.UnixNano) #44196
Comments
This was previously considered and rejected at #18935 and #27782. I still think it's worth doing, though. (I think the name would be One small thing that has changed since #27782 is that we added To recapitulate some points I made on #27782 after it was closed:
|
@willyrgf, what is the context where you need to do time calculations in Unix milliseconds? |
This proposal has been added to the active column of the proposals project |
At work, we use milliseconds in protobuf messages, databases, and log messages. I can't recall any usage of time.UnixNano() / int64(time.Millisecond)
time.UnixNano() / 1e6 This confuses new Go developers ("Am I doing this wrong? I must be missing something obvious.."), and makes normally straightforward code harder to mentally parse. Adding a dedicated user-defined library call to solve this problem isn't satisfactory:
|
Please have a look at package https://pkg.go.dev/github.com/ulikunitz/unixtime, which I wrote for #27782. On pkg.go.dev it has no reported imports, but on github.com there are 865 clones and 145 unique cloners. |
Used ms timestamps on a couple of php projects, never used nanos except in Go for converting to mill8s. |
JavaScript works in milliseconds; e.g. I've only written a few go-programs with considerable JavaScript-interactions, but in all of those, |
Another API option is func (Time) SinceUnix() time.Duration Better name welcome. Then you could write Or Other name ideas:
|
Thanks everyone for all suggestions, ideas and the questions. I'll edit the proposal and present a more completed proposal with all my arguments for each suggetions, ideas and questions sent here. |
Or |
@josharian I definitely prefer that suggestion, though my name suggestion would be @randall77's suggestion is not bad, though it's a bit awkward. |
Any method to compute Unix time in milliseconds from a nanosecond int64 value, which includes values of type Duration, limits the supported time range approximately from 1677-09-21 to 2262-04-11, which is much smaller than the Javascript range which is "approximately -273,790 to 273,790 years relative to 1970.". See https://tc39.es/ecma262/2020/#sec-time-values-and-time-range. Since Javascript is the driver for the millisecond requirement, I think this should be taken into consideration. But if the full int64 millisecond Unix time range is supported an additional function UnixMillis to compute a Time value from Unix time in milliseconds will be required. |
re "looking for some kind of a standard reason". We convert timestamps into milliseconds since the epoch (and vice versa) for talking to external APIs (I'm sure there are more, this is just what we happen to use, and which showed up when I grepped for the manual conversion):
I always think of this as a "representation of a timestamp" not as "the duration since 1970" (though I realize both definitions are equivalent), so the framing of I'd also love a symmetrical method to create a new time from a timestamp formatted this way, so that the conversion into and out-of this format is similarly easy.
Similar to @cespare, here, most uses of (Every time I have to implement this, I end up on this StackOverflow post (https://stackoverflow.com/questions/24122821/go-golang-time-now-unixnano-convert-to-milliseconds) which has a bunch of debate on whether the accepted answer is actually correct, or just happens to work given the way go is setup today...) |
I can confirm having done the same, including looking at SO first . I love @randall77 's solution in #44196 (comment) but I don't think it has come up on SO. For performance reasons, it might also be nice to avoid the float division of the naive |
@ConradIrwin thanks for those references, and thanks @msiebuhr for pointing out JavaScript. |
Why |
For better or for worse we should follow the existing example of |
Fair enough -- I noticed there's also |
There's more stuff in This is cluttering the package docs for everyone just to get rid of a single division in (some) user code. |
Change https://golang.org/cl/293349 mentions this issue: |
The CL that was sent originally included constructors time.UnixMilli and time.UnixMicro but they have been removed. I originally gave time.Unix two arguments to avoid having separate time.Unix and time.UnixNano. Does anyone object to this? |
Based on the discussion above, this proposal seems like a likely accept. |
I updated the proposal based on all discussions that took place here. |
Some tiny detail regarding the updated proposal: Please use µsec or at least usec for microsecond. The former uses the proper SI unit prefix and the latter is the typical abbreviation in programming languages restricted to ASCII. For example struct timeval of <sys/time.h> has a field tv_usec. The name mcsec appears at least to me quite unusual. |
In fact it's not a good name, I didn't think when I wrote this. |
No change in consensus, so accepted. 🎉 |
I updated https://golang.org/cl/293349 to match |
I would like to implement a function
time.UnixMills()
to return t in int64 of milliseconds, like in thetime.UnixNano()
returns t in int64 of nanoseconds.Today If I want to get the value of the time in milliseconds we need to do a lot of calculations, but I know other languages have this function and researching about that I've implemented that function in the time package.
It's a simple function with two const needed to easier understood.
Here is the simplest way that I've found to implement: https://go-review.googlesource.com/c/go/+/290772/1/src/time/time.go
This implementation is based on Rust
as_millis()
: https://doc.rust-lang.org/src/core/time.rs.html#393EDITED:
First, thanks all for the contributions with all discuss, I had some problems and I couldn't contribute much during the discussions and initially not being able to expose all my points. Now all points have been discussed and we apparently have a consensus.
Just to format the proposal correctly with what was agreed.
Based on the discussions we will have both directions of the converters, generators and consumers and we probably will have something like that:
The text was updated successfully, but these errors were encountered: