/
timeperiod.go
58 lines (52 loc) · 1.46 KB
/
timeperiod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package timeperiod
import (
"time"
)
// Seconds converts a time string with unit to an integer number of seconds
// Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`
func Seconds(period string) int {
duration, ok := convert(period)
if !ok {
return 0
}
return int(duration.Seconds())
}
// Milliseconds converts a time string with unit to an integer number of milliseconds
// Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`
func Milliseconds(period string) int {
duration, ok := convert(period)
if !ok {
return 0
}
nano := duration.Nanoseconds()
return int(nano / (1000 * 1000))
}
// Microseconds converts a time string with unit to an integer number of microseconds
// Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`
func Microseconds(period string) int {
duration, ok := convert(period)
if !ok {
return 0
}
nano := duration.Nanoseconds()
return int(nano / 1000)
}
// Nanoseconds converts a time string with unit to an integer number of nanoseconds
// Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`
func Nanoseconds(period string) int {
duration, ok := convert(period)
if !ok {
return 0
}
nano := duration.Nanoseconds()
return int(nano)
}
// Duration converts a time string with unit to time.Duration
// Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`
func Duration(period string) time.Duration {
duration, ok := convert(period)
if !ok {
return 0
}
return duration
}