Skip to content

model: add Duration unit constants and conversion methods#952

Open
locker95 wants to merge 2 commits into
prometheus:mainfrom
locker95:model-duration-milliseconds
Open

model: add Duration unit constants and conversion methods#952
locker95 wants to merge 2 commits into
prometheus:mainfrom
locker95:model-duration-milliseconds

Conversation

@locker95

Copy link
Copy Markdown

Summary

Extend model.Duration with unit constants and conversion methods so callers no longer need awkward casts to/from time.Duration.

Constants (mirroring time package, plus Day/Week which ParseDuration already supports):

  • model.Nanosecond, model.Microsecond, model.Millisecond
  • model.Second, model.Minute, model.Hour
  • model.Day, model.Week

Methods (same signatures as time.Duration):

  • Nanoseconds(), Microseconds(), Milliseconds()int64
  • Seconds(), Minutes(), Hours()float64

Before / after

// Before
int64(d) / int64(time.Millisecond)
15 * 24 * model.Duration(time.Hour)
model.Duration(time.Duration(n) * time.Millisecond)

// After
d.Milliseconds()
15 * model.Day
model.Duration(n) * model.Millisecond

Fixes #222

cc @roidelapluie @gotjosh

Test plan

  • go test ./model/ -run 'TestDuration' -count=1
  • New tests cover constant values vs time.* equivalents, multiply usage, conversion methods (including negative durations)

locker95 added 2 commits July 22, 2026 15:40
Working with model.Duration is awkward for simple conversions and
multiplications because callers must cast to/from time.Duration:

  int64(d)/int64(time.Millisecond)
  15 * 24 * model.Duration(time.Hour)
  model.Duration(n * time.Millisecond)

Add common unit constants (Nanosecond through Week) and conversion
methods (Nanoseconds through Hours) mirroring the standard time
package. Day and Week are included because ParseDuration already
supports those units.

Fixes prometheus#222

Signed-off-by: Dean Chen <862469039@qq.com>
…ants

Signed-off-by: Dean Chen <862469039@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend model.Duration with Miliseconds() method as well as model.{Milisecond,Second, ...} vars.

1 participant