-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathduration.go
199 lines (170 loc) · 4.81 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package mpd
import (
"encoding/xml"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// Duration is an extension of the original time.Duration. This type is used to
// re-format the String() output to support the ISO 8601 duration standard. And
// add the MarshalXMLAttr and UnmarshalXMLAttr functions.
type Duration time.Duration
var (
rStart = "^P" // Must start with a 'P'
rDays = "(\\d+D)?" // We only allow Days for durations, not Months or Years
rTime = "(?:T" // If there's any 'time' units then they must be preceded by a 'T'
rHours = "(\\d+H)?" // Hours
rMinutes = "(\\d+M)?" // Minutes
rSeconds = "([\\d.]+S)?" // Seconds (Potentially decimal)
rEnd = ")?$" // end of regex must close "T" capture group
)
var xmlDurationRegex = regexp.MustCompile(rStart + rDays + rTime + rHours + rMinutes + rSeconds + rEnd)
func (d *Duration) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: name, Value: d.String()}, nil
}
func (d *Duration) UnmarshalXMLAttr(attr xml.Attr) error {
dur, err := ParseDuration(attr.Value)
if err != nil {
return err
}
*d = Duration(dur)
return nil
}
// String returns a string representing the duration in the form "PT72H3M0.5S".
// Leading zero units are omitted. The zero duration formats as PT0S.
// Based on src/time/time.go's time.Duration.String function.
func (d *Duration) String() string {
// This is inlinable to take advantage of "function outlining".
// Thus, the caller can decide whether a string must be heap allocated.
var arr [32]byte
if d == nil {
return "PT0S"
}
n := d.format(&arr)
return "PT" + string(arr[n:])
}
// format formats the representation of d into the end of buf and returns the
// offset of the first character. This function is modified to use the iso 1801
// duration standard. This standard only uses the "H", "M", "S" characters.
// // Based on src/time/time.go's time.Duration.Format function.
func (d *Duration) format(buf *[32]byte) int {
// Largest time is 2540400h10m10.000000000s
w := len(buf)
u := uint64(*d)
neg := *d < 0
if neg {
u = -u
}
w--
buf[w] = 'S'
w, u = fmtFrac(buf[:w], u, 9)
// u is now integer seconds
w = fmtInt(buf[:w], u%60)
u /= 60
// u is now integer minutes
if u > 0 {
w--
buf[w] = 'M'
w = fmtInt(buf[:w], u%60)
u /= 60
// u is now integer hours
// Stop at hours because days can be different lengths.
if u > 0 {
w--
buf[w] = 'H'
w = fmtInt(buf[:w], u)
}
}
if neg {
w--
buf[w] = '-'
}
return w
}
// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the
// tail of buf, omitting trailing zeros. it omits the decimal
// point too when the fraction is 0. It returns the index where the
// output bytes begin and the value v/10**prec.
// Copied from src/time/time.go.
func fmtFrac(buf []byte, v uint64, prec int) (nw int, nv uint64) {
// Omit trailing zeros up to and including decimal point.
w := len(buf)
print := false
for i := 0; i < prec; i++ {
digit := v % 10
print = print || digit != 0
if print {
w--
buf[w] = byte(digit) + '0'
}
v /= 10
}
if print {
w--
buf[w] = '.'
}
return w, v
}
// fmtInt formats v into the tail of buf.
// It returns the index where the output begins.
// Copied from src/time/time.go.
func fmtInt(buf []byte, v uint64) int {
w := len(buf)
if v == 0 {
w--
buf[w] = '0'
} else {
for v > 0 {
w--
buf[w] = byte(v%10) + '0'
v /= 10
}
}
return w
}
func ParseDuration(str string) (time.Duration, error) {
if len(str) < 3 {
return 0, errors.New("at least one number and designator are required")
}
if strings.Contains(str, "-") {
return 0, errors.New("duration cannot be negative")
}
// Check that only the parts we expect exist and that everything's in the correct order
if !xmlDurationRegex.Match([]byte(str)) {
return 0, errors.New("duration must be in the format: P[nD][T[nH][nM][nS]]")
}
var parts = xmlDurationRegex.FindStringSubmatch(str)
var total time.Duration
if parts[1] != "" {
days, err := strconv.Atoi(strings.TrimRight(parts[1], "D"))
if err != nil {
return 0, fmt.Errorf("error parsing Days: %s", err)
}
total += time.Duration(days) * time.Hour * 24
}
if parts[2] != "" {
hours, err := strconv.Atoi(strings.TrimRight(parts[2], "H"))
if err != nil {
return 0, fmt.Errorf("error parsing Hours: %s", err)
}
total += time.Duration(hours) * time.Hour
}
if parts[3] != "" {
mins, err := strconv.Atoi(strings.TrimRight(parts[3], "M"))
if err != nil {
return 0, fmt.Errorf("error parsing Minutes: %s", err)
}
total += time.Duration(mins) * time.Minute
}
if parts[4] != "" {
secs, err := strconv.ParseFloat(strings.TrimRight(parts[4], "S"), 64)
if err != nil {
return 0, fmt.Errorf("error parsing Seconds: %s", err)
}
total += time.Duration(secs * float64(time.Second))
}
return total, nil
}