Skip to content

Commit

Permalink
Merge pull request #15 from cbsinteractive/duration_fix
Browse files Browse the repository at this point in the history
Duration fix MT-2670
  • Loading branch information
bpogy authored Dec 18, 2023
2 parents 020cffd + 178c130 commit 0701e40
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/zencoder/go-dash/v3

go 1.16
go 1.16
29 changes: 8 additions & 21 deletions mpd/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,19 @@ func (d *Duration) String() string {
u = -u
}

if u < uint64(time.Second) {
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--
// time.Duration & zerocoder packages convert seconds to nano ('nS'), micro ('µS') or milliseconds ('mS') with corresponding designation
// Fix for "Duration must be in the format: P[nD][T[nH][nM][nS]]" error is to keep seconds.
if u == 0 {
return "PT0S"
} else {
du := float64(u) / float64(time.Second)
s := fmt.Sprintf("%2f", du)
sf := strings.TrimRight(s, "0")
return fmt.Sprintf("PT%sS", sf)
}
/*
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'
Expand Down
9 changes: 6 additions & 3 deletions mpd/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (

func TestDuration(t *testing.T) {
in := map[string]string{
"0s": "PT0S",
"6m16s": "PT6M16S",
"1.97s": "PT1.97S",
"0s": "PT0S",
"6m16s": "PT6M16S",
"1.97s": "PT1.97S",
"0.0021s": "PT0.0021S",
"0.0000021s": "PT0.000002S",
"0.021s": "PT0.021S",
}
for ins, ex := range in {
timeDur, err := time.ParseDuration(ins)
Expand Down

0 comments on commit 0701e40

Please sign in to comment.