Skip to content

Implement to unmarshal PT duration #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions mpd/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package mpd

import (
"encoding/xml"
"errors"
"strconv"
"strings"
"time"
)

Expand All @@ -13,6 +16,15 @@ 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
Expand Down Expand Up @@ -126,3 +138,80 @@ func fmtInt(buf []byte, v uint64) int {
}
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
}
20 changes: 20 additions & 0 deletions mpd/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ func TestDuration(t *testing.T) {
assert.Equal(t, ex, dur.String())
}
}

func TestParseDuration(t *testing.T) {
in := map[string]float64{
"PT0S": 0,
"PT1M": 60,
"PT2H": 7200,
"PT6M16S": 376,
"PT1.97S": 1.97,
"PT1H2M3.456S": 3723.456,
"P1DT2H": (26 * time.Hour).Seconds(),
"PT20M": (20 * time.Minute).Seconds(),
"-P60D": -(60 * 24 * time.Hour).Seconds(),
"PT1M30.5S": (time.Minute + 30*time.Second + 500*time.Millisecond).Seconds(),
}
for ins, ex := range in {
act, err := parseDuration(ins)
assert.NoError(t, err, ins)
assert.Equal(t, ex, act.Seconds(), ins)
}
}