From 58352ea69d3e40bd3c91eacc7bbd48f58d5f2c8a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 29 Aug 2014 23:22:46 +0200 Subject: [PATCH] db/Stats: use std::chrono::duration for the total duration Use milliseconds precision to reduce rounding errors. --- src/Stats.cxx | 7 +++++-- src/db/Helpers.cxx | 2 +- src/db/Stats.hxx | 6 ++++-- src/db/plugins/ProxyDatabasePlugin.cxx | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Stats.cxx b/src/Stats.cxx index 8fc626ecb..39d371ace 100644 --- a/src/Stats.cxx +++ b/src/Stats.cxx @@ -99,15 +99,18 @@ db_stats_print(Client &client, const Database &db) if (!stats_update(db)) return; + unsigned total_duration_s = + std::chrono::duration_cast(stats.total_duration).count(); + client_printf(client, "artists: %u\n" "albums: %u\n" "songs: %u\n" - "db_playtime: %lu\n", + "db_playtime: %u\n", stats.artist_count, stats.album_count, stats.song_count, - stats.total_duration); + total_duration_s); const time_t update_stamp = db.GetUpdateStamp(); if (update_stamp > 0) diff --git a/src/db/Helpers.cxx b/src/db/Helpers.cxx index 8df6c265c..add4bb98e 100644 --- a/src/db/Helpers.cxx +++ b/src/db/Helpers.cxx @@ -41,7 +41,7 @@ StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums, const Tag &tag) { if (!tag.duration.IsNegative()) - stats.total_duration += tag.duration.ToS(); + stats.total_duration += tag.duration; for (const auto &item : tag) { switch (item.type) { diff --git a/src/db/Stats.hxx b/src/db/Stats.hxx index 107af9d5f..131a5dc47 100644 --- a/src/db/Stats.hxx +++ b/src/db/Stats.hxx @@ -20,6 +20,8 @@ #ifndef MPD_DATABASE_STATS_HXX #define MPD_DATABASE_STATS_HXX +#include "Chrono.hxx" + struct DatabaseStats { /** * Number of songs. @@ -29,7 +31,7 @@ struct DatabaseStats { /** * Total duration of all songs (in seconds). */ - unsigned long total_duration; + std::chrono::duration total_duration; /** * Number of distinct artist names. @@ -43,7 +45,7 @@ struct DatabaseStats { void Clear() { song_count = 0; - total_duration = 0; + total_duration = total_duration.zero(); artist_count = album_count = 0; } }; diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index 97b544203..8f9d12188 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -807,7 +807,7 @@ ProxyDatabase::GetStats(const DatabaseSelection &selection, update_stamp = (time_t)mpd_stats_get_db_update_time(stats2); stats.song_count = mpd_stats_get_number_of_songs(stats2); - stats.total_duration = mpd_stats_get_db_play_time(stats2); + stats.total_duration = std::chrono::seconds(mpd_stats_get_db_play_time(stats2)); stats.artist_count = mpd_stats_get_number_of_artists(stats2); stats.album_count = mpd_stats_get_number_of_albums(stats2); mpd_stats_free(stats2);