From 4a077922ae90ec3a33db8d4d309821954c9fea7d Mon Sep 17 00:00:00 2001 From: casey langen Date: Tue, 26 Jan 2021 15:25:38 -0800 Subject: [PATCH] Added the ability to format durations with hours. --- src/musikcore/support/Duration.cpp | 31 +++++++++++++++++++++- src/musikcore/support/Duration.h | 1 + src/musikcube/app/window/TrackListView.cpp | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/musikcore/support/Duration.cpp b/src/musikcore/support/Duration.cpp index 089a4b6e0..300ea7147 100755 --- a/src/musikcore/support/Duration.cpp +++ b/src/musikcore/support/Duration.cpp @@ -42,7 +42,29 @@ static std::string formatDuration(N seconds) { N mins = (seconds / 60); N secs = seconds - (mins * 60); char buffer[128]; - snprintf(buffer, sizeof(buffer), "%d:%02d", narrow_cast(mins), narrow_cast(secs)); + snprintf( + buffer, + sizeof(buffer), + "%d:%02d", + narrow_cast(mins), + narrow_cast(secs)); + return std::string(buffer); +} + +template +static std::string formatDurationWithHours(N seconds) { + N hours = (seconds / 3600); + seconds -= hours * 3600; + N mins = (seconds / 60); + N secs = seconds - (mins * 60); + char buffer[128]; + snprintf( + buffer, + sizeof(buffer), + "%d:%02d:%02d", + narrow_cast(hours), + narrow_cast(mins), + narrow_cast(secs)); return std::string(buffer); } @@ -69,4 +91,11 @@ namespace musik { namespace core { namespace duration { return "0:00"; } + std::string DurationWithHours(size_t seconds) { + if (seconds < 3600) { + return formatDuration(seconds); + } + return formatDurationWithHours(seconds); + } + } } } diff --git a/src/musikcore/support/Duration.h b/src/musikcore/support/Duration.h index 555d01ffd..ecdec4dd2 100755 --- a/src/musikcore/support/Duration.h +++ b/src/musikcore/support/Duration.h @@ -42,5 +42,6 @@ namespace musik { namespace core { namespace duration { std::string Duration(int seconds); std::string Duration(size_t seconds); std::string Duration(double seconds); + std::string DurationWithHours(size_t seconds); } } } diff --git a/src/musikcube/app/window/TrackListView.cpp b/src/musikcube/app/window/TrackListView.cpp index cc4480e9d..729bcf01d 100755 --- a/src/musikcube/app/window/TrackListView.cpp +++ b/src/musikcube/app/window/TrackListView.cpp @@ -503,7 +503,7 @@ IScrollAdapter::EntryPtr TrackListView::Adapter::GetEntry(cursespp::ScrollableWi auto duration = this->parent.headers.DurationFromAdapterIndex(rawIndex); if (duration > 0) { - album += " - " + core::duration::Duration(duration); + album += " - " + core::duration::DurationWithHours(duration); } album = text::Ellipsize(album, this->GetWidth());