Skip to content

Commit

Permalink
Will implement duration parser (#8)
Browse files Browse the repository at this point in the history
* Implement duration parser
* Fix example
* Increase coverage to 100%
  • Loading branch information
mxpv authored and eduncan911 committed Dec 10, 2017
1 parent f14c4d7 commit 10213c6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,5 @@ func ExampleItem_AddDuration() {

fmt.Println(i.IDuration)
// Output:
// 533
// 8:53
}
33 changes: 25 additions & 8 deletions podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 19 additions & 0 deletions podcast_internals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 10213c6

Please sign in to comment.