Skip to content

Commit a14d955

Browse files
committed
model: add Duration unit constants and conversion methods
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 #222 Signed-off-by: Dean Chen <862469039@qq.com>
1 parent b63d8c0 commit a14d955

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

model/time.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,47 @@ func (t *Time) UnmarshalJSON(b []byte) error {
164164
// This type should not propagate beyond the scope of input/output processing.
165165
type Duration time.Duration
166166

167+
// Common durations. Day and Week are included because ParseDuration supports
168+
// those units (unlike the standard time package).
169+
//
170+
// To count the number of units in a Duration, divide:
171+
//
172+
// second := model.Second
173+
// fmt.Print(int64(second/model.Millisecond)) // prints 1000
174+
//
175+
// To convert an integer number of units to a Duration, multiply:
176+
//
177+
// seconds := 10
178+
// fmt.Print(model.Duration(seconds)*model.Second) // prints 10s
179+
const (
180+
Nanosecond Duration = 1
181+
Microsecond = 1000 * Nanosecond
182+
Millisecond = 1000 * Microsecond
183+
Second = 1000 * Millisecond
184+
Minute = 60 * Second
185+
Hour = 60 * Minute
186+
Day = 24 * Hour
187+
Week = 7 * Day
188+
)
189+
190+
// Nanoseconds returns the duration as an integer nanosecond count.
191+
func (d Duration) Nanoseconds() int64 { return time.Duration(d).Nanoseconds() }
192+
193+
// Microseconds returns the duration as an integer microsecond count.
194+
func (d Duration) Microseconds() int64 { return time.Duration(d).Microseconds() }
195+
196+
// Milliseconds returns the duration as an integer millisecond count.
197+
func (d Duration) Milliseconds() int64 { return time.Duration(d).Milliseconds() }
198+
199+
// Seconds returns the duration as a floating point number of seconds.
200+
func (d Duration) Seconds() float64 { return time.Duration(d).Seconds() }
201+
202+
// Minutes returns the duration as a floating point number of minutes.
203+
func (d Duration) Minutes() float64 { return time.Duration(d).Minutes() }
204+
205+
// Hours returns the duration as a floating point number of hours.
206+
func (d Duration) Hours() float64 { return time.Duration(d).Hours() }
207+
167208
// Set implements pflag/flag.Value.
168209
func (d *Duration) Set(s string) error {
169210
var err error

model/time_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,41 @@ func TestDuration(t *testing.T) {
6767
require.Equalf(t, delta, duration, "Expected %s to be equal to %s", delta, duration)
6868
}
6969

70+
func TestDurationConstants(t *testing.T) {
71+
require.Equal(t, Duration(time.Nanosecond), Nanosecond)
72+
require.Equal(t, Duration(time.Microsecond), Microsecond)
73+
require.Equal(t, Duration(time.Millisecond), Millisecond)
74+
require.Equal(t, Duration(time.Second), Second)
75+
require.Equal(t, Duration(time.Minute), Minute)
76+
require.Equal(t, Duration(time.Hour), Hour)
77+
require.Equal(t, Duration(24*time.Hour), Day)
78+
require.Equal(t, Duration(7*24*time.Hour), Week)
79+
80+
// Typical usage from the issue: multiply integers by model constants.
81+
require.Equal(t, Duration(15*24*time.Hour), 15*Day)
82+
require.Equal(t, Duration(5000*time.Millisecond), 5000*Millisecond)
83+
}
84+
85+
func TestDurationMethods(t *testing.T) {
86+
d := 2*Hour + 30*Minute + 15*Second + 250*Millisecond
87+
td := time.Duration(d)
88+
89+
require.Equal(t, td.Nanoseconds(), d.Nanoseconds())
90+
require.Equal(t, td.Microseconds(), d.Microseconds())
91+
require.Equal(t, td.Milliseconds(), d.Milliseconds())
92+
require.Equal(t, td.Seconds(), d.Seconds())
93+
require.Equal(t, td.Minutes(), d.Minutes())
94+
require.Equal(t, td.Hours(), d.Hours())
95+
96+
require.Equal(t, int64(2*60*60*1000+30*60*1000+15*1000+250), d.Milliseconds())
97+
require.Equal(t, int64(1500), (1500 * Microsecond).Microseconds())
98+
require.Equal(t, int64(1500), (1500 * Nanosecond).Nanoseconds())
99+
100+
// Negative durations.
101+
require.Equal(t, int64(-1500), (-1500 * Millisecond).Milliseconds())
102+
require.Equal(t, -1.5, (-1500 * Millisecond).Seconds())
103+
}
104+
70105
func TestParseDuration(t *testing.T) {
71106
type testCase struct {
72107
in string

0 commit comments

Comments
 (0)