-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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 Duration-conversion functions Milliseconds() and Microseconds() #28564
Comments
@ianlancetaylor those were for different functions, but I mentioned them above too. The idea of a library to measure popularity is interesting, but I don't see how people would know it exists unless they come across these GH issues (which probably explains the low Star count on https://github.com/ulikunitz/unixtime).
Not that I know of; my main motivation was to lay out a case for why these should exist. |
I'm sympathetic, since it's so easy to mistype the division (e.g. you have to remember NOT to type t.Nanoseconds() / 1000 for milliseconds). The way you get float would be t.Seconds() * 1000 which is easy to remember. |
Per discussion with @golang/proposal-review, we agree this should be added. @griesemer suggests |
The return values are integers, as opposed to floats, since the fractionals can be derived from multiplying t.Seconds(). Fixes golang#28564
Change https://golang.org/cl/167387 mentions this issue: |
I propose adding these two
Duration
-conversion functions totime.go
:(these were already proposed in #5491 as
int64
and rejected as WorkingAsIntended)Here's a map of
Duration
-conversion helpers intime.go
-- all added in efe3d35 -- missing the two functions above:Duration
constantsDuration
conversiontime.Nanosecond
func (d Duration) Nanoseconds() int64
time.Microsecond
time.Millisecond
time.Second
func (d Duration) Seconds() float64
time.Minute
func (d Duration) Minutes() float64
time.Hour
func (d Duration) Hours() float64
Reasons
time
API. Reasons I've seen for not implementing these is generally "just divide by Millisecond", but this results in application code that would be written with two different styles of conversion, e.g.time.Since(time.Now()).Hours()
vstime.Since(time.Now()) / Millisecond
Milliseconds()
helper.Side-effects
If these are added, it might lead to suggestions for other similarly
time.go
functions:(t *Time).msec()
and(t *Time).usec()
(t Time).Millisecond()
and(t Time).Microsecond()
(t Time).UnixMilli()
and(t Time).UnixMicro()
(e.g. proposal: time: direct support for Unix millis and micros - RE: #18935 #27782, proposal: time: direct support for Unix millis and micros #18935)What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
time.Since(time.Now()).Milliseconds()
What did you expect to see?
Some value representing milliseconds elapsed.
What did you see instead?
Compilation error:
"...Milliseconds undefined (type time.Duration has no field or method Milliseconds)"
The text was updated successfully, but these errors were encountered: