Skip to content
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

Implement duration parser #8

Merged
merged 3 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -453,12 +453,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)
}
18 changes: 18 additions & 0 deletions podcast_internals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ 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, "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))
}