diff --git a/examples_test.go b/examples_test.go index 13c0913..9bb61a2 100644 --- a/examples_test.go +++ b/examples_test.go @@ -256,5 +256,5 @@ func ExampleItem_AddDuration() { fmt.Println(i.IDuration) // Output: - // 533 + // 8:53 } diff --git a/podcast.go b/podcast.go index 02ab7c6..4703d2e 100644 --- a/podcast.go +++ b/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) } diff --git a/podcast_internals_test.go b/podcast_internals_test.go index 48fd5b3..9aba932 100644 --- a/podcast_internals_test.go +++ b/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)) +}