From 7df8e1eab27c9539acad3f843d8a882ea9b3e71c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 29 Aug 2014 23:18:40 +0200 Subject: db/Count: use std::chrono::duration for the total time Use milliseconds precision to reduce rounding errors. --- src/db/Count.cxx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/db/Count.cxx b/src/db/Count.cxx index fecd901e5..b58ac53e9 100644 --- a/src/db/Count.cxx +++ b/src/db/Count.cxx @@ -30,10 +30,10 @@ struct SearchStats { unsigned n_songs; - unsigned long total_time_s; + std::chrono::duration total_duration; constexpr SearchStats() - :n_songs(0), total_time_s(0) {} + :n_songs(0), total_duration(0) {} }; class TagCountMap : public std::map { @@ -42,10 +42,13 @@ class TagCountMap : public std::map { static void PrintSearchStats(Client &client, const SearchStats &stats) { + unsigned total_duration_s = + std::chrono::duration_cast(stats.total_duration).count(); + client_printf(client, "songs: %u\n" - "playtime: %lu\n", - stats.n_songs, stats.total_time_s); + "playtime: %u\n", + stats.n_songs, total_duration_s); } static void @@ -67,7 +70,7 @@ stats_visitor_song(SearchStats &stats, const LightSong &song) const auto duration = song.GetDuration(); if (!duration.IsNegative()) - stats.total_time_s += duration.ToS(); + stats.total_duration += duration; return true; } @@ -83,7 +86,7 @@ CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag) SearchStats &s = r.first->second; ++s.n_songs; if (!tag.duration.IsNegative()) - s.total_time_s += tag.duration.ToS(); + s.total_duration += tag.duration; found = true; } -- cgit v1.2.3