Skip to content

time: add Duration-conversion functions Milliseconds() and Microseconds()  #28564

Closed
@tiegz

Description

@tiegz

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.ProposalProposal-Accepted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions