Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions model/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,34 @@ func (t *Time) UnmarshalJSON(b []byte) error {
// This type should not propagate beyond the scope of input/output processing.
type Duration time.Duration

// Common durations. Day and Week are included because ParseDuration supports
// those units (unlike the standard time package).
//
// To count the number of units in a Duration, divide:
//
// second := model.Second
// fmt.Print(int64(second/model.Millisecond)) // prints 1000
//
// To convert an integer number of units to a Duration, multiply:
//
// seconds := 10
// fmt.Print(model.Duration(seconds)*model.Second) // prints 10s
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
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

)

// Milliseconds returns the duration as an integer millisecond count.
// Prometheus stores timestamps in milliseconds; time.Duration already
// covers the other units.
func (d Duration) Milliseconds() int64 { return time.Duration(d).Milliseconds() }
Comment thread
bwplotka marked this conversation as resolved.

// Set implements pflag/flag.Value.
func (d *Duration) Set(s string) error {
var err error
Expand Down
23 changes: 23 additions & 0 deletions model/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ func TestDuration(t *testing.T) {
require.Equalf(t, delta, duration, "Expected %s to be equal to %s", delta, duration)
}

func TestDurationConstants(t *testing.T) {
// require.Equal(expected, actual) — expected is the reference time.Duration cast.
require.Equal(t, Nanosecond, Duration(time.Nanosecond))
require.Equal(t, Microsecond, Duration(time.Microsecond))
require.Equal(t, Millisecond, Duration(time.Millisecond))
require.Equal(t, Second, Duration(time.Second))
require.Equal(t, Minute, Duration(time.Minute))
require.Equal(t, Hour, Duration(time.Hour))
require.Equal(t, Day, Duration(24*time.Hour))
require.Equal(t, Week, Duration(7*24*time.Hour))

// Typical usage from the issue: multiply integers by model constants.
require.Equal(t, 15*Day, Duration(15*24*time.Hour))
require.Equal(t, 5000*Millisecond, Duration(5000*time.Millisecond))
}

func TestDurationMilliseconds(t *testing.T) {
d := 2*Hour + 30*Minute + 15*Second + 250*Millisecond
require.Equal(t, time.Duration(d).Milliseconds(), d.Milliseconds())
require.Equal(t, int64(2*60*60*1000+30*60*1000+15*1000+250), d.Milliseconds())
require.Equal(t, int64(-1500), (-1500 * Millisecond).Milliseconds())
}

func TestParseDuration(t *testing.T) {
type testCase struct {
in string
Expand Down
Loading