comparison src/core/time.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 7cf53145de11
children f4538a4c91ba
comparison
equal deleted inserted replaced
304:2115488eb302 305:91ac90a34003
7 #include <ctime> 7 #include <ctime>
8 #include <string> 8 #include <string>
9 9
10 namespace Time { 10 namespace Time {
11 11
12 Duration::Duration(int64_t l) { 12 static int64_t GetSecondsInMinutes(Timestamp length) {
13 length = l; 13 return std::llround(static_cast<double>(length) / 60.0);
14 } 14 }
15 15
16 std::string Duration::AsRelativeString() { 16 static int64_t GetSecondsInHours(Timestamp length) {
17 return std::llround(static_cast<double>(length) / 3600.0);
18 }
19
20 static int64_t GetSecondsInDays(Timestamp length) {
21 return std::llround(static_cast<double>(length) / 86400.0);
22 }
23
24 std::string GetSecondsAsRelativeString(Timestamp length) {
17 std::string result; 25 std::string result;
18 26
19 auto get = [](int64_t val, const std::string& s, const std::string& p) { 27 auto get = [](int64_t val, const std::string& s, const std::string& p) {
20 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p); 28 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p);
21 }; 29 };
22 30
23 if (InSeconds() < 60) 31 if (length < 60)
24 result = get(InSeconds(), "second", "seconds"); 32 result = get(length, "second", "seconds");
25 else if (InMinutes() < 60) 33 else if (GetSecondsInMinutes(length) < 60)
26 result = get(InMinutes(), "minute", "minutes"); 34 result = get(GetSecondsInMinutes(length), "minute", "minutes");
27 else if (InHours() < 24) 35 else if (GetSecondsInHours(length) < 24)
28 result = get(InHours(), "hour", "hours"); 36 result = get(GetSecondsInHours(length), "hour", "hours");
29 else if (InDays() < 28) 37 else if (GetSecondsInDays(length) < 28)
30 result = get(InDays(), "day", "days"); 38 result = get(GetSecondsInDays(length), "day", "days");
31 else if (InDays() < 365) 39 else if (GetSecondsInDays(length) < 365)
32 result = get(InDays() / 30, "month", "months"); 40 result = get(GetSecondsInDays(length) / 30, "month", "months");
33 else 41 else
34 result = get(InDays() / 365, "year", "years"); 42 result = get(GetSecondsInDays(length) / 365, "year", "years");
35 43
36 if (length < 0) 44 if (length < 0)
37 result = "In " + result; 45 result = "In " + result;
38 else 46 else
39 result += " ago"; 47 result += " ago";
40 48
41 return result; 49 return result;
42 } 50 }
43 51
44 int64_t Duration::InSeconds() { 52 std::string GetSecondsAsAbsoluteString(Units unit_cutoff, Timestamp amount, double unit_in_seconds) {
45 return length; 53 /* avoid calculating this twice */
46 } 54 const double years_conv = (31556952.0 / unit_in_seconds);
55 const double months_conv = (2629746.0 / unit_in_seconds);
56 const double days_conv = (86400.0 / unit_in_seconds);
57 const double hours_conv = (3600.0 / unit_in_seconds);
58 const double minutes_conv = (60.0 / unit_in_seconds);
59 const double seconds_conv = (1.0 / unit_in_seconds);
47 60
48 int64_t Duration::InMinutes() { 61 const int years = amount / years_conv;
49 return std::llround(static_cast<double>(length) / 60.0); 62 const int months = std::fmod(amount, years_conv) / months_conv;
50 } 63 const int days = std::fmod(amount, months_conv) / days_conv;
64 const int hours = std::fmod(amount, days_conv) / hours_conv;
65 const int minutes = std::fmod(amount, hours_conv) / minutes_conv;
66 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv;
51 67
52 int64_t Duration::InHours() { 68 const auto add_time_segment = [](std::ostringstream& str, int64_t amount, const std::string_view& singular,
53 return std::llround(static_cast<double>(length) / 3600.0); 69 const std::string_view& plural, bool always = false) {
54 } 70 if (amount > 0 || always)
71 str << amount << ((amount == 1) ? singular : plural);
72 };
55 73
56 int64_t Duration::InDays() { 74 /* for now, this function is very en_US specific */
57 return std::llround(static_cast<double>(length) / 86400.0); 75
76 std::ostringstream string;
77 add_time_segment(string, years, " year ", " years ");
78 add_time_segment(string, months, " month ", " months ");
79 add_time_segment(string, days, " day ", " days ");
80 add_time_segment(string, hours, " hour ", " hours ");
81
82 if (unit_cutoff == Units::Minutes) {
83 add_time_segment(string, minutes, " minute", " minutes", true);
84 return string.str();
85 } else {
86 add_time_segment(string, minutes, " minute ", " minutes ");
87 }
88
89 add_time_segment(string, seconds, " second", " seconds", true);
90 return string.str();
58 } 91 }
59 92
60 int64_t GetSystemTime() { 93 int64_t GetSystemTime() {
61 static_assert(sizeof(int64_t) >= sizeof(time_t)); 94 static_assert(sizeof(int64_t) >= sizeof(time_t));
62 time_t t = std::time(nullptr); 95 time_t t = std::time(nullptr);
63 return *reinterpret_cast<int64_t*>(&t); 96 return reinterpret_cast<int64_t>(t);
64 } 97 }
65 98
66 } // namespace Time 99 } // namespace Time