comparison src/core/time.cc @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents d928ec7b6a0d
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
9 9
10 #include <QDateTime> 10 #include <QDateTime>
11 11
12 namespace Time { 12 namespace Time {
13 13
14 static Timestamp GetSecondsInMinutes(Timestamp length) { 14 static Timestamp GetSecondsInMinutes(Timestamp length)
15 {
15 return std::llround(static_cast<double>(length) / 60.0); 16 return std::llround(static_cast<double>(length) / 60.0);
16 } 17 }
17 18
18 static Timestamp GetSecondsInHours(Timestamp length) { 19 static Timestamp GetSecondsInHours(Timestamp length)
20 {
19 return std::llround(static_cast<double>(length) / 3600.0); 21 return std::llround(static_cast<double>(length) / 3600.0);
20 } 22 }
21 23
22 static Timestamp GetSecondsInDays(Timestamp length) { 24 static Timestamp GetSecondsInDays(Timestamp length)
25 {
23 return std::llround(static_cast<double>(length) / 86400.0); 26 return std::llround(static_cast<double>(length) / 86400.0);
24 } 27 }
25 28
26 std::string GetSecondsAsRelativeString(Timestamp length) { 29 std::string GetSecondsAsRelativeString(Timestamp length)
30 {
27 std::string result; 31 std::string result;
28 32
29 auto get = [](Timestamp val, const std::string& s, const std::string& p) { 33 auto get = [](Timestamp val, const std::string &s, const std::string &p) {
30 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p); 34 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p);
31 }; 35 };
32 36
33 if (length < 60) 37 if (length < 60)
34 result = get(length, "second", "seconds"); 38 result = get(length, "second", "seconds");
51 return result; 55 return result;
52 } 56 }
53 57
54 /* "amount" does not have to be in seconds, and can be any unit if the correct ratio to seconds 58 /* "amount" does not have to be in seconds, and can be any unit if the correct ratio to seconds
55 * is passed to "unit_in_seconds" (for example, if the input is minutes, pass 60.0) */ 59 * is passed to "unit_in_seconds" (for example, if the input is minutes, pass 60.0) */
56 std::string GetSecondsAsAbsoluteString(Units unit_cutoff, Timestamp amount, double unit_in_seconds) { 60 std::string GetSecondsAsAbsoluteString(Units unit_cutoff, Timestamp amount, double unit_in_seconds)
61 {
57 /* avoid calculating this twice */ 62 /* avoid calculating this twice */
58 const double years_conv = (31556952.0 / unit_in_seconds); 63 const double years_conv = (31556952.0 / unit_in_seconds);
59 const double months_conv = (2629746.0 / unit_in_seconds); 64 const double months_conv = (2629746.0 / unit_in_seconds);
60 const double days_conv = (86400.0 / unit_in_seconds); 65 const double days_conv = (86400.0 / unit_in_seconds);
61 const double hours_conv = (3600.0 / unit_in_seconds); 66 const double hours_conv = (3600.0 / unit_in_seconds);
67 const int days = std::fmod(amount, months_conv) / days_conv; 72 const int days = std::fmod(amount, months_conv) / days_conv;
68 const int hours = std::fmod(amount, days_conv) / hours_conv; 73 const int hours = std::fmod(amount, days_conv) / hours_conv;
69 const int minutes = std::fmod(amount, hours_conv) / minutes_conv; 74 const int minutes = std::fmod(amount, hours_conv) / minutes_conv;
70 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv; 75 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv;
71 76
72 const auto add_time_segment = [](std::ostringstream& str, int64_t amount, const std::string_view& singular, 77 const auto add_time_segment = [](std::ostringstream &str, int64_t amount, const std::string_view &singular,
73 const std::string_view& plural, bool always = false) { 78 const std::string_view &plural, bool always = false) {
74 if (amount > 0 || always) 79 if (amount > 0 || always)
75 str << amount << ((amount == 1) ? singular : plural); 80 str << amount << ((amount == 1) ? singular : plural);
76 }; 81 };
77 82
78 /* for now, this function is very en_US specific */ 83 /* for now, this function is very en_US specific */
92 97
93 add_time_segment(string, seconds, " second", " seconds", true); 98 add_time_segment(string, seconds, " second", " seconds", true);
94 return string.str(); 99 return string.str();
95 } 100 }
96 101
97 Timestamp GetSystemTime() { 102 Timestamp GetSystemTime()
103 {
98 return QDateTime::currentDateTime().toUTC().toSecsSinceEpoch(); 104 return QDateTime::currentDateTime().toUTC().toSecsSinceEpoch();
99 } 105 }
100 106
101 Timestamp ParseISO8601Time(const std::string& str) { 107 Timestamp ParseISO8601Time(const std::string &str)
108 {
102 return QDateTime::fromString(Strings::ToQString(str), Qt::ISODateWithMs).toUTC().toSecsSinceEpoch(); 109 return QDateTime::fromString(Strings::ToQString(str), Qt::ISODateWithMs).toUTC().toSecsSinceEpoch();
103 } 110 }
104 111
105 } // namespace Time 112 } // namespace Time