-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
114 lines (98 loc) · 2.51 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package time
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// Common durations. There is no definition for units of Day or larger
// to avoid confusion across daylight savings time zone transitions.
//
// To count the number of units in a Duration, divide:
//
// second := time.Second
// fmt.Print(int64(second/time.Millisecond)) // prints 1000
//
// To convert an integer number of units to a Duration, multiply:
//
// seconds := 10
// fmt.Print(time.Duration(seconds)*time.Second) // prints 10s
//
// NOTE: This is a legacy compatibility layer for the time package.
const (
Nanosecond time.Duration = 1
Microsecond = 1000 * time.Nanosecond
Millisecond = 1000 * time.Microsecond
Second = 1000 * time.Millisecond
Minute = 60 * time.Second
Hour = 60 * time.Minute
Day = 24 * time.Hour
)
var (
daysRegex = regexp.MustCompile(`^(\d+)d\w*$`)
)
type Duration time.Duration
func (d Duration) Duration() time.Duration {
return time.Duration(d)
}
func (d Duration) MarshalText() ([]byte, error) {
return []byte(time.Duration(d).String()), nil
}
func (d *Duration) UnmarshalText(b []byte) error {
return d.unmarshalText(string(b))
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var unmarshalledJSON any
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.UseNumber()
if err := decoder.Decode(&unmarshalledJSON); err != nil {
return err
}
switch t := unmarshalledJSON.(type) {
case json.Number:
dur, err := t.Int64()
if err != nil {
return err
}
*d = Duration(dur)
case string:
return d.unmarshalText(t)
default:
return fmt.Errorf("invalid type for duration: %#v", unmarshalledJSON)
}
return nil
}
func (d *Duration) unmarshalText(text string) error {
days := daysRegex.FindAllStringSubmatch(text, -1)
if len(days) == 0 || len(days[0]) != 2 {
v, err := time.ParseDuration(text)
if err != nil {
return err
}
*d = Duration(v)
return nil
}
// Text contains days.
i, err := strconv.Atoi(days[0][1])
if err != nil {
return fmt.Errorf("invalid duration: %w", err)
}
*d = Duration(Day * time.Duration(i))
// Remove days from text and continue the typical way.
rest := strings.TrimPrefix(text, days[0][1]+"d")
if len(rest) == 0 {
return nil
}
v, err := time.ParseDuration(rest)
if err != nil {
return err
}
*d += Duration(v)
return nil
}