/
ext_duration.go
69 lines (63 loc) · 1.75 KB
/
ext_duration.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
59
60
61
62
63
64
65
66
67
68
69
package settings
import (
"fmt"
"time"
)
// ExtDuration is an extension of time.Duration to support units longer than hours.
type ExtDuration struct {
Quantity int64
Unit rune
}
// ParseExtDuration parses an ExtDuration from a string.
func ParseExtDuration(value string) (ExtDuration, error) {
e := ExtDuration{}
if n, err := fmt.Sscanf(value, "%d%c", &e.Quantity, &e.Unit); n != 2 {
return e, fmt.Errorf("Invalid value %q for duration", value)
} else if err != nil {
return e, fmt.Errorf("Invalid value %q for duration: %v", value, err)
}
switch e.Unit {
case 'd', 'D', 'w', 'W', 'm', 'M', 'y', 'Y':
break
default:
return e, fmt.Errorf("Invalid unit \"%c\" for threshold", e.Unit)
}
return e, nil
}
// Duration returns the value as a time.Duration.
func (e ExtDuration) Duration() time.Duration {
day := 24 * time.Hour
switch e.Unit {
case 'd', 'D':
return time.Duration(e.Quantity) * day
case 'w', 'W':
return time.Duration(e.Quantity) * 7 * day
case 'm', 'M':
return time.Duration(e.Quantity) * 31 * day
case 'y', 'Y':
return time.Duration(e.Quantity) * 356 * day
default:
panic("invalid duration unit")
}
}
// String returns a string representation of the ExtDuration.
func (e ExtDuration) String() string {
switch e.Unit {
case 'd', 'D':
return fmt.Sprintf("%d %s", e.Quantity, pluralize(e.Quantity, "day"))
case 'w', 'W':
return fmt.Sprintf("%d %s", e.Quantity, pluralize(e.Quantity, "week"))
case 'm', 'M':
return fmt.Sprintf("%d %s", e.Quantity, pluralize(e.Quantity, "month"))
case 'y', 'Y':
return fmt.Sprintf("%d %s", e.Quantity, pluralize(e.Quantity, "year"))
default:
panic("invalid duration unit")
}
}
func pluralize(count int64, value string) string {
if count == 1 {
return value
}
return value + "s"
}