-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
299 lines (234 loc) · 5.33 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package mpd
import (
"encoding/xml"
"strconv"
"time"
"github.com/pkg/errors"
)
// Duration naively implements the xsd:duration format defined by https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html#duration
// N.B.: It is not intended to serve as a general xsd:duration as it does not implement the month side of xsd:duration.
type Duration struct {
ns time.Duration
m int
}
// A copy of various time.Duration constants.
const (
Day = 24 * time.Hour
Hour = time.Hour
Minute = time.Minute
Second = time.Second
Millisecond = time.Millisecond
Microsecond = time.Microsecond
Nanosecond = time.Nanosecond
)
// Duration returns the mpd.Duration value as a time.Duration.
//
// If Duration contains a non-zero value for the month side of the xsd:duration,
// then this function will return an error noting that the value is out of range..
func (d Duration) Duration() (time.Duration, error) {
if d.m != 0 {
return 0, errors.New("value out of range")
}
return d.ns, nil
}
// Add returns the mpd.Duration plus the given time.Duration.
func (d Duration) Add(dur time.Duration) Duration {
d.ns += dur
return d
}
// Scale returns the mpd.Duration scaled by the given time.Duration.
func (d Duration) Scale(dur time.Duration) Duration {
d.ns *= dur
return d
}
// AddToTime returns the given time.Time plus the given mpd.Duration value..
func (d Duration) AddToTime(t time.Time) time.Time {
if d.m != 0 {
t = t.AddDate(0, d.m, 0)
}
return t.Add(d.ns)
}
// MarshalXMLAttr implements xml.MarshalerAttr.
func (d Duration) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if s := d.XMLString(); s != "" {
return xml.Attr{
Name: name,
Value: s,
}, nil
}
return xml.Attr{}, nil
}
// NewDuration returns an mpd.Duration corresponding to the length in months and time.Duration.
//
// It returns an error if the sign of two arguments are different.
func NewDuration(months int, d time.Duration) (Duration, error) {
if (d < 0 && months > 0) || (d > 0 && months < 0) {
return Duration{}, errors.New("invalid duration")
}
return Duration{
ns: d,
m: months,
}, nil
}
// XMLString returns the XML representation of the Duration as a string.
func (d Duration) XMLString() string {
var b []byte
dur := d.ns
m := int64(d.m)
if dur < 0 {
dur = -dur
m = -m
b = append(b, '-')
}
b = append(b, 'P')
seconds, frac := int64(dur/Second), int64(dur%Second)
minutes, seconds := seconds/60, seconds%60
hours, minutes := minutes/60, minutes%60
days, hours := hours/24, hours%24
years, months := m/12, m%12
if years > 0 {
b = strconv.AppendInt(b, years, 10)
b = append(b, 'Y')
}
if months > 0 {
b = strconv.AppendInt(b, months, 10)
b = append(b, 'M')
}
if days > 0 {
b = strconv.AppendInt(b, days, 10)
b = append(b, 'D')
}
if hours > 0 || minutes > 0 || seconds > 0 || frac > 0 {
b = append(b, 'T')
if hours > 0 {
b = strconv.AppendInt(b, hours, 10)
b = append(b, 'H')
}
if minutes > 0 {
b = strconv.AppendInt(b, minutes, 10)
b = append(b, 'M')
}
if seconds+frac != 0 || hours+minutes == 0 {
b = strconv.AppendInt(b, seconds, 10)
if frac > 0 {
b = append(b, '.')
f := strconv.AppendInt(nil, frac, 10)
if len(f) < 9 {
nb := make([]byte, 9-len(f))
nb[0] = '0'
bp := 1
for bp < len(nb) {
copy(nb[bp:], nb[:bp])
bp *= 2
}
b = append(b, nb...)
}
l := len(f)
for f[l-1] == '0' {
l--
}
b = append(b, f[:l]...)
}
b = append(b, 'S')
}
}
return string(b)
}
var durationScales = []time.Duration{
Second,
100 * Millisecond,
10 * Millisecond,
Millisecond,
100 * Microsecond,
10 * Microsecond,
Microsecond,
100 * Nanosecond,
10 * Nanosecond,
Nanosecond,
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr.
func (d *Duration) UnmarshalXMLAttr(attr xml.Attr) error {
var dur time.Duration
var months int
v := attr.Value
if len(v) < 1 {
return errors.New("invalid duration")
}
var neg bool
if v[0] == '-' {
neg = true
v = v[1:]
}
if len(v) < 1 || v[0] != 'P' {
return errors.New("invalid duration")
}
var n time.Duration
var radix bool
var scale uint
var t, end bool
for _, r := range v[1:] {
if end {
// we had extra chars after a fractional value, this is an error
return errors.New("invalid duration")
}
switch r {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
n = (n * 10) + time.Duration(r-'0')
if radix {
scale++
}
case 'T':
if t {
return errors.New("invalid duration")
}
t = true
case '.':
if radix {
return errors.New("invalid duration")
}
radix = true
case 'Y':
if t || radix {
return errors.New("invalid duration")
}
months += int(n) * 12
n = 0
case 'D':
if t || radix {
return errors.New("invalid duration")
}
dur += n * Day
n = 0
case 'H':
if !t || radix {
return errors.New("invalid duration")
}
dur += n * Hour
n = 0
case 'M':
switch {
case radix:
return errors.New("invalid duration")
case t:
dur += n * Minute
default:
months += int(n)
}
n = 0
case 'S':
if !t || scale > 9 {
return errors.New("invalid duration")
}
dur += n * durationScales[scale]
end = true
default:
return errors.New("invalid duration")
}
}
if neg {
dur = -dur
}
d.ns = dur
d.m = months
return nil
}