Skip to content

Commit fb82bae

Browse files
committed
Ignore duration with Years and Months
1 parent 04fd8f5 commit fb82bae

File tree

2 files changed

+20
-40
lines changed

2 files changed

+20
-40
lines changed

mpd/duration.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ func parseDuration(str string) (time.Duration, error) {
156156
}
157157
offset++
158158

159-
base := time.Unix(0, 0)
160-
t := base
161-
162159
var dateStr, timeStr string
163160
if i := strings.IndexByte(str[offset:], 'T'); i != -1 {
164161
dateStr = str[offset : offset+i]
@@ -167,38 +164,27 @@ func parseDuration(str string) (time.Duration, error) {
167164
dateStr = str[offset:]
168165
}
169166

167+
var sum float64
170168
if len(dateStr) > 0 {
171-
var pos int
172-
var err error
173-
var years, months, days int
174-
if i := strings.IndexByte(dateStr[pos:], 'Y'); i != -1 {
175-
years, err = strconv.Atoi(dateStr[pos : pos+i])
176-
if err != nil {
177-
return 0, err
178-
}
179-
pos += i + 1
169+
if i := strings.IndexByte(dateStr, 'Y'); i != -1 {
170+
return 0, errors.New("input duration contains Years notation")
180171
}
181172

182-
if i := strings.IndexByte(dateStr[pos:], 'M'); i != -1 {
183-
months, err = strconv.Atoi(dateStr[pos : pos+i])
184-
if err != nil {
185-
return 0, err
186-
}
187-
pos += i + 1
173+
if i := strings.IndexByte(dateStr, 'M'); i != -1 {
174+
return 0, errors.New("input duration contains Months notation")
188175
}
189176

190-
if i := strings.IndexByte(dateStr[pos:], 'D'); i != -1 {
191-
days, err = strconv.Atoi(dateStr[pos : pos+i])
177+
if i := strings.IndexByte(dateStr, 'D'); i != -1 {
178+
days, err := strconv.Atoi(dateStr[0:i])
192179
if err != nil {
193180
return 0, err
194181
}
182+
sum += float64(days) * 86400
195183
}
196-
t = t.AddDate(years, months, days)
197184
}
198185

199186
if len(timeStr) > 0 {
200187
var pos int
201-
var sum float64
202188
if i := strings.IndexByte(timeStr[pos:], 'H'); i != -1 {
203189
hours, err := strconv.ParseInt(timeStr[pos:pos+i], 10, 64)
204190
if err != nil {
@@ -222,11 +208,10 @@ func parseDuration(str string) (time.Duration, error) {
222208
}
223209
sum += seconds
224210
}
225-
t = t.Add(time.Duration(sum * float64(time.Second)))
226211
}
227212

228213
if minus {
229-
return -t.Sub(base), nil
214+
sum = -sum
230215
}
231-
return t.Sub(base), nil
216+
return time.Duration(sum * float64(time.Second)), nil
232217
}

mpd/duration_test.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,16 @@ func TestDuration(t *testing.T) {
2323

2424
func TestParseDuration(t *testing.T) {
2525
in := map[string]float64{
26-
"PT0S": 0,
27-
"PT1M": 60,
28-
"PT2H": 7200,
29-
"PT6M16S": 376,
30-
"PT1.97S": 1.97,
31-
"PT1H2M3.456S": 3723.456,
32-
"P1YT2M3S": (365*24*time.Hour + 2*time.Minute + 3*time.Second).Seconds(),
33-
"P1Y2M3DT12H34M56.78S": (365*24*time.Hour + 59*24*time.Hour + 3*24*time.Hour + 12*time.Hour + 34*time.Minute + 56*time.Second + 780*time.Millisecond).Seconds(),
34-
"P1DT2H": (26 * time.Hour).Seconds(),
35-
"P20M": (608 * 24 * time.Hour).Seconds(),
36-
"PT20M": (20 * time.Minute).Seconds(),
37-
"P0Y20M0D": (608 * 24 * time.Hour).Seconds(),
38-
"P0Y": 0,
39-
"-P60D": -(60 * 24 * time.Hour).Seconds(),
40-
"PT1M30.5S": (time.Minute + 30*time.Second + 500*time.Millisecond).Seconds(),
26+
"PT0S": 0,
27+
"PT1M": 60,
28+
"PT2H": 7200,
29+
"PT6M16S": 376,
30+
"PT1.97S": 1.97,
31+
"PT1H2M3.456S": 3723.456,
32+
"P1DT2H": (26 * time.Hour).Seconds(),
33+
"PT20M": (20 * time.Minute).Seconds(),
34+
"-P60D": -(60 * 24 * time.Hour).Seconds(),
35+
"PT1M30.5S": (time.Minute + 30*time.Second + 500*time.Millisecond).Seconds(),
4136
}
4237
for ins, ex := range in {
4338
act, err := parseDuration(ins)

0 commit comments

Comments
 (0)