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

Open
tiegz opened this Issue Nov 2, 2018 · 5 comments

Comments

Projects
None yet
6 participants
@tiegz

tiegz commented Nov 2, 2018

I propose adding these two Duration-conversion functions to time.go:

func (d Duration) Milliseconds() float64
func (d Duration) Microseconds() float64

(these were already proposed in #5491 as int64 and rejected as WorkingAsIntended)

Here's a map of Duration-conversion helpers in time.go -- all added in efe3d35 -- missing the two functions above:

Unit Duration constants Duration conversion
Nanosecond time.Nanosecond func (d Duration) Nanoseconds() int64
Microsecond time.Microsecond
Millisecond time.Millisecond
Second time.Second func (d Duration) Seconds() float64
Minute time.Minute func (d Duration) Minutes() float64
Hour time.Hour func (d Duration) Hours() float64

Reasons

  • assymetry: as shown above, there's an asymmetry in the 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() vs time.Since(time.Now()) / Millisecond
  • newcomer perspective: as a newcomer to Go this year, I found it helpful that Go has a duration type, but was confused why it doesn't have a Milliseconds() helper.
  • usefulness: milliseconds (and microseconds) are common units for measuring web response times, database response times, high-frequency trading, gaming, etc (e.g. Google research, High Scalability blog), justifying their inclusion.
  • demand: I've found some other accounts of people who want these functions: example, example, example
  • language parity: lastly, Rust -- another relatively new/growing language -- has these two conversion functions in its Duration type

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. #27782, #18935)

What version of Go are you using (go version)?

go version go1.11 darwin/amd64

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)"

@tiegz tiegz changed the title from time: add Duration is missing Milliseconds() and Microseconds() getters #5491 to time: add Duration-conversion functions Milliseconds() and Microseconds() Nov 2, 2018

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Nov 2, 2018

Also proposed and rejected in #18935 and #27782. The latter issue recommends creating a third party library to see if it is popular. Has anything changed since those earlier discussions?

@tiegz

This comment has been minimized.

tiegz commented Nov 3, 2018

@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).

Has anything changed since those earlier discussions?

Not that I know of; my main motivation was to lay out a case for why these should exist.

@dmitshur dmitshur added this to the Unplanned milestone Nov 4, 2018

@agnivade agnivade changed the title from time: add Duration-conversion functions Milliseconds() and Microseconds() to proposal: time: add Duration-conversion functions Milliseconds() and Microseconds() Nov 6, 2018

@gopherbot gopherbot added the Proposal label Nov 6, 2018

@rsc

This comment has been minimized.

Contributor

rsc commented Nov 28, 2018

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).
My concern in 2013 in #5491 was the return type.
If you call Micro/Milli, do you most often want it in int64 or float64?
Do other language APIs agree on which to use?

The way you get float would be t.Seconds() * 1000 which is easy to remember.
Maybe that's an argument for providing the integer form, since that's the one with the unexpected constant in the calculation.

@andybons

This comment has been minimized.

Member

andybons commented Dec 5, 2018

Per discussion with @golang/proposal-review, we agree this should be added. @griesemer suggests int64 as the return type (you can always get the fractional by multiplying the time.Second output). This detail can be determined during review, however.

@andybons andybons changed the title from proposal: time: add Duration-conversion functions Milliseconds() and Microseconds() to time: add Duration-conversion functions Milliseconds() and Microseconds() Dec 5, 2018

@andybons andybons modified the milestones: Unplanned, Go1.13 Dec 5, 2018

@tiegz

This comment has been minimized.

tiegz commented Dec 5, 2018

Thanks @andybons !

Just to answer @rsc 's question, I mostly picked float64 because to be consistent with Seconds(), Minutes(), and Hours(). int64 would be nice since you could just search-and-replace duration / time.Millisecond with duration.Milliseconds() though, so it's a toss-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment