Mercurial > minori
diff src/gui/pages/statistics.cc @ 305:91ac90a34003
core/time: remove Duration class, use regular functions instead
this class was pretty useless anyway
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 19 May 2024 15:56:20 -0400 |
parents | b82841e76e79 |
children | 6b0768158dcd |
line wrap: on
line diff
--- a/src/gui/pages/statistics.cc Mon May 13 17:02:35 2024 -0400 +++ b/src/gui/pages/statistics.cc Sun May 19 15:56:20 2024 -0400 @@ -2,6 +2,7 @@ #include "core/anime_db.h" #include "core/session.h" #include "core/strings.h" +#include "core/time.h" #include "gui/pages/anime_list.h" #include "gui/widgets/graph.h" #include "gui/widgets/text.h" @@ -16,11 +17,6 @@ #include <cmath> #include <sstream> -enum class TimeUnits { - SECONDS, - MINUTES -}; - StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { setBackgroundRole(QPalette::Base); @@ -79,54 +75,6 @@ UpdateStatistics(); } -/* [in] enum TimeUnits unit: - * which unit to stop on - * [in] int amount: - * amount of units to parse - * [in, defaults to 1.0] double unit_in_seconds: - * equivalent of one of 'amount' in seconds, e.g. minutes would be 60.0 - */ -static std::string TimeToDateString(TimeUnits unit, int amount, double unit_in_seconds = 1.0) { - /* avoid calculating this twice */ - const double years_conv = (31556952.0 / unit_in_seconds); - const double months_conv = (2629746.0 / unit_in_seconds); - const double days_conv = (86400.0 / unit_in_seconds); - const double hours_conv = (3600.0 / unit_in_seconds); - const double minutes_conv = (60.0 / unit_in_seconds); - const double seconds_conv = (1.0 / unit_in_seconds); - - const int years = amount / years_conv; - const int months = std::fmod(amount, years_conv) / months_conv; - const int days = std::fmod(amount, months_conv) / days_conv; - const int hours = std::fmod(amount, days_conv) / hours_conv; - const int minutes = std::fmod(amount, hours_conv) / minutes_conv; - const int seconds = std::fmod(amount, minutes_conv) / seconds_conv; - - const auto add_time_segment = [](std::ostringstream& str, int amount, const std::string_view& singular, - const std::string_view& plural, bool always = false) { - if (amount > 0 || always) - str << amount << ((amount == 1) ? singular : plural); - }; - - /* for now, this function is very en_US specific */ - - std::ostringstream string; - add_time_segment(string, years, " year ", " years "); - add_time_segment(string, months, " month ", " months "); - add_time_segment(string, days, " day ", " days "); - add_time_segment(string, hours, " hour ", " hours "); - - if (unit == TimeUnits::MINUTES) { - add_time_segment(string, minutes, " minute", " minutes", true); - return string.str(); - } else { - add_time_segment(string, minutes, " minute ", " minutes "); - } - - add_time_segment(string, seconds, " second", " seconds", true); - return string.str(); -} - inline int GetTotalWithScore(const int score) { int count = 0; for (const auto& item : Anime::db.items) @@ -141,8 +89,8 @@ QTextStream ts(&string); ts << Anime::db.GetTotalAnimeAmount() << '\n'; ts << Anime::db.GetTotalEpisodeAmount() << '\n'; - ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n'; - ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n'; + ts << Strings::ToQString(Time::GetSecondsAsAbsoluteString(Time::Units::Minutes, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n'; + ts << Strings::ToQString(Time::GetSecondsAsAbsoluteString(Time::Units::Minutes, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n'; ts << Anime::db.GetAverageScore() << '\n'; ts << Anime::db.GetScoreDeviation(); _anime_list->GetData()->setText(string); @@ -152,7 +100,7 @@ _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); string = ""; - ts << Strings::ToQString(TimeToDateString(TimeUnits::SECONDS, session.uptime() / 1000)) << '\n'; + ts << Strings::ToQString(Time::GetSecondsAsAbsoluteString(Time::Units::Seconds, session.uptime() / 1000)) << '\n'; ts << session.GetRequests(); /* Application */ // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));