Browse Source

Will implement duration parser (#8)

* Implement duration parser
* Fix example
* Increase coverage to 100%
master
Maksym Pavlenko 5 years ago committed by Eric Duncan
parent
commit
10213c61ce
  1. 2
      examples_test.go
  2. 33
      podcast.go
  3. 19
      podcast_internals_test.go

2
examples_test.go

@ -256,5 +256,5 @@ func ExampleItem_AddDuration() {
fmt.Println(i.IDuration)
// Output:
// 533
// 8:53
}

33
podcast.go

@ -456,12 +456,29 @@ var parseAuthorNameEmail = func(a *Author) string {
}
var parseDuration = func(duration int64) string {
// TODO: parse the output into iTunes nicely formatted version.
//
// iTunes supports the following:
// HH:MM:SS
// H:MM:SS
// MM:SS
// M:SS
return strconv.FormatInt(duration, 10)
h := duration / 3600
duration = duration % 3600
m := duration / 60
duration = duration % 60
s := duration
// HH:MM:SS
if h > 9 {
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
// H:MM:SS
if h > 0 {
return fmt.Sprintf("%d:%02d:%02d", h, m, s)
}
// MM:SS
if m > 9 {
return fmt.Sprintf("%02d:%02d", m, s)
}
// M:SS
return fmt.Sprintf("%d:%02d", m, s)
}

19
podcast_internals_test.go

@ -40,3 +40,22 @@ func TestEncodeError(t *testing.T) {
// assert
assert.Error(t, err)
}
func TestParseDuration(t *testing.T) {
t.Parallel()
assert.Equal(t, "0:00", parseDuration(0))
assert.Equal(t, "0:40", parseDuration(40))
assert.Equal(t, "1:00", parseDuration(60))
assert.Equal(t, "1:40", parseDuration(100))
assert.Equal(t, "2:01", parseDuration(121))
assert.Equal(t, "59:59", parseDuration(3599))
assert.Equal(t, "1:00:00", parseDuration(3600))
assert.Equal(t, "1:00:01", parseDuration(3601))
assert.Equal(t, "1:01:00", parseDuration(3660))
assert.Equal(t, "1:01:03", parseDuration(3663))
assert.Equal(t, "10:00:00", parseDuration(36000))
assert.Equal(t, "10:00:01", parseDuration(36001))
assert.Equal(t, "10:01:00", parseDuration(36060))
assert.Equal(t, "10:01:03", parseDuration(36063))
}

Loading…
Cancel
Save