-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathduration.go
83 lines (71 loc) · 1.65 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package fly
import (
"encoding/json"
"fmt"
"time"
)
type Duration struct {
time.Duration
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
return d.ParseDuration(v)
}
func (d *Duration) UnmarshalTOML(v any) error {
return d.ParseDuration(v)
}
func (d Duration) MarshalTOML() ([]byte, error) {
v := fmt.Sprintf("\"%s\"", d.Duration.String())
return []byte(v), nil
}
func (d *Duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}
func (d *Duration) UnmarshalText(text []byte) error {
return d.ParseDuration(text)
}
func (d *Duration) ParseDuration(v any) error {
if v == nil {
d.Duration = 0
return nil
}
switch value := v.(type) {
case int64:
d.Duration = time.Duration(value)
case float64:
d.Duration = time.Duration(int64(value))
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
default:
return fmt.Errorf("Unknown duration type: %T", value)
}
return nil
}
// Compile parses a duration and returns, if successful, a Duration object.
func ParseDuration(v any) (*Duration, error) {
d := &Duration{}
if err := d.ParseDuration(v); err != nil {
return nil, err
}
return d, nil
}
// MustParseDuration is like ParseDuration but panics if the expression cannot be parsed.
// It simplifies safe initialization of global variables holding durations
// Same idea than regexp.MustCompile
func MustParseDuration(v any) *Duration {
d, err := ParseDuration(v)
if err != nil {
panic(err)
}
return d
}