Mercurial > minori
annotate src/core/time.cc @ 327:b5d6c27c308f
anime: refactor Anime::SeriesSeason to Season class
ToLocalString has also been altered to take in both season
and year because lots of locales actually treat formatting
seasons differently! most notably is Russian which adds a
suffix at the end to notate seasons(??)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 13 Jun 2024 01:49:18 -0400 |
parents | d928ec7b6a0d |
children |
rev | line source |
---|---|
9 | 1 #include "core/time.h" |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
188
diff
changeset
|
2 #include "core/strings.h" |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
188
diff
changeset
|
3 |
9 | 4 #include <cassert> |
5 #include <cmath> | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
6 #include <cstdint> |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
7 #include <ctime> |
9 | 8 #include <string> |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
9 |
308 | 10 #include <QDateTime> |
11 | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
12 namespace Time { |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
13 |
308 | 14 static Timestamp GetSecondsInMinutes(Timestamp length) { |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
15 return std::llround(static_cast<double>(length) / 60.0); |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
16 } |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
17 |
308 | 18 static Timestamp GetSecondsInHours(Timestamp length) { |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
19 return std::llround(static_cast<double>(length) / 3600.0); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
20 } |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
21 |
308 | 22 static Timestamp GetSecondsInDays(Timestamp length) { |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
23 return std::llround(static_cast<double>(length) / 86400.0); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
24 } |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
25 |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
26 std::string GetSecondsAsRelativeString(Timestamp length) { |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
27 std::string result; |
9 | 28 |
308 | 29 auto get = [](Timestamp val, const std::string& s, const std::string& p) { |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
188
diff
changeset
|
30 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p); |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
31 }; |
9 | 32 |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
33 if (length < 60) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
34 result = get(length, "second", "seconds"); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
35 else if (GetSecondsInMinutes(length) < 60) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
36 result = get(GetSecondsInMinutes(length), "minute", "minutes"); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
37 else if (GetSecondsInHours(length) < 24) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
38 result = get(GetSecondsInHours(length), "hour", "hours"); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
39 else if (GetSecondsInDays(length) < 28) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
40 result = get(GetSecondsInDays(length), "day", "days"); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
41 else if (GetSecondsInDays(length) < 365) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
42 result = get(GetSecondsInDays(length) / 30, "month", "months"); |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
43 else |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
44 result = get(GetSecondsInDays(length) / 365, "year", "years"); |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
45 |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
46 if (length < 0) |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
47 result = "In " + result; |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
48 else |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
49 result += " ago"; |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
50 |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
51 return result; |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
52 } |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
53 |
306
f4538a4c91ba
cleanup: remove extraneous executable flags
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
54 /* "amount" does not have to be in seconds, and can be any unit if the correct ratio to seconds |
f4538a4c91ba
cleanup: remove extraneous executable flags
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
55 * is passed to "unit_in_seconds" (for example, if the input is minutes, pass 60.0) */ |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
56 std::string GetSecondsAsAbsoluteString(Units unit_cutoff, Timestamp amount, double unit_in_seconds) { |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
57 /* avoid calculating this twice */ |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
58 const double years_conv = (31556952.0 / unit_in_seconds); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
59 const double months_conv = (2629746.0 / unit_in_seconds); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
60 const double days_conv = (86400.0 / unit_in_seconds); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
61 const double hours_conv = (3600.0 / unit_in_seconds); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
62 const double minutes_conv = (60.0 / unit_in_seconds); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
63 const double seconds_conv = (1.0 / unit_in_seconds); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
64 |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
65 const int years = amount / years_conv; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
66 const int months = std::fmod(amount, years_conv) / months_conv; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
67 const int days = std::fmod(amount, months_conv) / days_conv; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
68 const int hours = std::fmod(amount, days_conv) / hours_conv; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
69 const int minutes = std::fmod(amount, hours_conv) / minutes_conv; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
70 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv; |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
71 |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
72 const auto add_time_segment = [](std::ostringstream& str, int64_t amount, const std::string_view& singular, |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
73 const std::string_view& plural, bool always = false) { |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
74 if (amount > 0 || always) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
75 str << amount << ((amount == 1) ? singular : plural); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
76 }; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
77 |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
78 /* for now, this function is very en_US specific */ |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
79 |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
80 std::ostringstream string; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
81 add_time_segment(string, years, " year ", " years "); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
82 add_time_segment(string, months, " month ", " months "); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
83 add_time_segment(string, days, " day ", " days "); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
84 add_time_segment(string, hours, " hour ", " hours "); |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
85 |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
86 if (unit_cutoff == Units::Minutes) { |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
87 add_time_segment(string, minutes, " minute", " minutes", true); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
88 return string.str(); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
89 } else { |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
90 add_time_segment(string, minutes, " minute ", " minutes "); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
91 } |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
92 |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
93 add_time_segment(string, seconds, " second", " seconds", true); |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
94 return string.str(); |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
95 } |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
96 |
308 | 97 Timestamp GetSystemTime() { |
98 return QDateTime::currentDateTime().toUTC().toSecsSinceEpoch(); | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
99 } |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
100 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
308
diff
changeset
|
101 Timestamp ParseISO8601Time(const std::string& str) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
308
diff
changeset
|
102 return QDateTime::fromString(Strings::ToQString(str), Qt::ISODateWithMs).toUTC().toSecsSinceEpoch(); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
308
diff
changeset
|
103 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
308
diff
changeset
|
104 |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
188
diff
changeset
|
105 } // namespace Time |