forked from zencoder/go-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduration.go
217 lines (192 loc) · 4.17 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
// based on code from golang src/time/time.go
package mpd
import (
"encoding/xml"
"errors"
"strconv"
"strings"
"time"
)
type Duration time.Duration
func (d Duration) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{name, 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 renders a Duration in XML Duration Data Type format
func (d *Duration) String() string {
// Largest time is 2540400h10m10.000000000s
var buf [32]byte
w := len(buf)
u := uint64(*d)
neg := *d < 0
if neg {
u = -u
}
if u < uint64(time.Second) {
// Special case: if duration is smaller than a second,
// use smaller units, like 1.2ms
var prec int
w--
buf[w] = 'S'
w--
if u == 0 {
return "PT0S"
}
/*
switch {
case u < uint64(Millisecond):
// print microseconds
prec = 3
// U+00B5 'µ' micro sign == 0xC2 0xB5
w-- // Need room for two bytes.
copy(buf[w:], "µ")
default:
// print milliseconds
prec = 6
buf[w] = 'm'
}
*/
w, u = fmtFrac(buf[:w], u, prec)
w = fmtInt(buf[:w], u)
} else {
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 "PT" + string(buf[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.
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.
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("input duration too short")
}
var minus bool
offset := 0
if str[offset] == '-' {
minus = true
offset++
}
if str[offset] != 'P' {
return 0, errors.New("input duration does not have a valid prefix")
}
offset++
var dateStr, timeStr string
if i := strings.IndexByte(str[offset:], 'T'); i != -1 {
dateStr = str[offset : offset+i]
timeStr = str[offset+i+1:]
} else {
dateStr = str[offset:]
}
var sum float64
if len(dateStr) > 0 {
if i := strings.IndexByte(dateStr, 'Y'); i != -1 {
return 0, errors.New("input duration contains Years notation")
}
if i := strings.IndexByte(dateStr, 'M'); i != -1 {
return 0, errors.New("input duration contains Months notation")
}
if i := strings.IndexByte(dateStr, 'D'); i != -1 {
days, err := strconv.Atoi(dateStr[0:i])
if err != nil {
return 0, err
}
sum += float64(days) * 86400
}
}
if len(timeStr) > 0 {
var pos int
if i := strings.IndexByte(timeStr[pos:], 'H'); i != -1 {
hours, err := strconv.ParseInt(timeStr[pos:pos+i], 10, 64)
if err != nil {
return 0, err
}
sum += float64(hours) * 3600
pos += i + 1
}
if i := strings.IndexByte(timeStr[pos:], 'M'); i != -1 {
minutes, err := strconv.ParseInt(timeStr[pos:pos+i], 10, 64)
if err != nil {
return 0, err
}
sum += float64(minutes) * 60
pos += i + 1
}
if i := strings.IndexByte(timeStr[pos:], 'S'); i != -1 {
seconds, err := strconv.ParseFloat(timeStr[pos:pos+i], 64)
if err != nil {
return 0, err
}
sum += seconds
}
}
if minus {
sum = -sum
}
return time.Duration(sum * float64(time.Second)), nil
}