Skip to content

model: add Duration unit constants and conversion methods - #952

Merged
bwplotka merged 3 commits into
prometheus:mainfrom
locker95:model-duration-milliseconds
Aug 26, 2026
Merged

model: add Duration unit constants and conversion methods#952
bwplotka merged 3 commits into
prometheus:mainfrom
locker95:model-duration-milliseconds

Conversation

@locker95

Copy link
Copy Markdown
Contributor

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>

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread model/time.go
Comment thread model/time.go
Minute = 60 * Second
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we also define a year, should we add it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skipped it. ParseDuration already takes y, and a calendar year isn't a fixed duration like day/week.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but we treat it as fixed in some calculations, anyway let's skip to not surface confusion

Drop the extra time.Duration wrappers. Prometheus timestamps are
milliseconds; Day/Week constants stay because ParseDuration supports
those units.

Signed-off-by: Dean Chen <862469039@qq.com>
@bwplotka
bwplotka merged commit eb72e27 into prometheus:main Aug 26, 2026
8 checks passed
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.

2 participants