db/Count: use std::chrono::duration for the total time

Use milliseconds precision to reduce rounding errors.
This commit is contained in:
Max Kellermann 2014-08-29 23:18:40 +02:00
parent 3158955198
commit 7df8e1eab2

View File

@ -30,10 +30,10 @@
struct SearchStats { struct SearchStats {
unsigned n_songs; unsigned n_songs;
unsigned long total_time_s; std::chrono::duration<std::uint64_t, SongTime::period> total_duration;
constexpr SearchStats() constexpr SearchStats()
:n_songs(0), total_time_s(0) {} :n_songs(0), total_duration(0) {}
}; };
class TagCountMap : public std::map<std::string, SearchStats> { class TagCountMap : public std::map<std::string, SearchStats> {
@ -42,10 +42,13 @@ class TagCountMap : public std::map<std::string, SearchStats> {
static void static void
PrintSearchStats(Client &client, const SearchStats &stats) PrintSearchStats(Client &client, const SearchStats &stats)
{ {
unsigned total_duration_s =
std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();
client_printf(client, client_printf(client,
"songs: %u\n" "songs: %u\n"
"playtime: %lu\n", "playtime: %u\n",
stats.n_songs, stats.total_time_s); stats.n_songs, total_duration_s);
} }
static void static void
@ -67,7 +70,7 @@ stats_visitor_song(SearchStats &stats, const LightSong &song)
const auto duration = song.GetDuration(); const auto duration = song.GetDuration();
if (!duration.IsNegative()) if (!duration.IsNegative())
stats.total_time_s += duration.ToS(); stats.total_duration += duration;
return true; return true;
} }
@ -83,7 +86,7 @@ CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag)
SearchStats &s = r.first->second; SearchStats &s = r.first->second;
++s.n_songs; ++s.n_songs;
if (!tag.duration.IsNegative()) if (!tag.duration.IsNegative())
s.total_time_s += tag.duration.ToS(); s.total_duration += tag.duration;
found = true; found = true;
} }