Mercurial > minori
comparison src/core/time.cc @ 308:da2c5a8ff306
time: don't use time_t!
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Tue, 11 Jun 2024 04:24:19 -0400 |
| parents | f4538a4c91ba |
| children | d928ec7b6a0d |
comparison
equal
deleted
inserted
replaced
| 307:8769c5d50b06 | 308:da2c5a8ff306 |
|---|---|
| 5 #include <cmath> | 5 #include <cmath> |
| 6 #include <cstdint> | 6 #include <cstdint> |
| 7 #include <ctime> | 7 #include <ctime> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include <QDateTime> | |
| 11 | |
| 10 namespace Time { | 12 namespace Time { |
| 11 | 13 |
| 12 static int64_t GetSecondsInMinutes(Timestamp length) { | 14 static Timestamp GetSecondsInMinutes(Timestamp length) { |
| 13 return std::llround(static_cast<double>(length) / 60.0); | 15 return std::llround(static_cast<double>(length) / 60.0); |
| 14 } | 16 } |
| 15 | 17 |
| 16 static int64_t GetSecondsInHours(Timestamp length) { | 18 static Timestamp GetSecondsInHours(Timestamp length) { |
| 17 return std::llround(static_cast<double>(length) / 3600.0); | 19 return std::llround(static_cast<double>(length) / 3600.0); |
| 18 } | 20 } |
| 19 | 21 |
| 20 static int64_t GetSecondsInDays(Timestamp length) { | 22 static Timestamp GetSecondsInDays(Timestamp length) { |
| 21 return std::llround(static_cast<double>(length) / 86400.0); | 23 return std::llround(static_cast<double>(length) / 86400.0); |
| 22 } | 24 } |
| 23 | 25 |
| 24 std::string GetSecondsAsRelativeString(Timestamp length) { | 26 std::string GetSecondsAsRelativeString(Timestamp length) { |
| 25 std::string result; | 27 std::string result; |
| 26 | 28 |
| 27 auto get = [](int64_t val, const std::string& s, const std::string& p) { | 29 auto get = [](Timestamp val, const std::string& s, const std::string& p) { |
| 28 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p); | 30 return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p); |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 if (length < 60) | 33 if (length < 60) |
| 32 result = get(length, "second", "seconds"); | 34 result = get(length, "second", "seconds"); |
| 90 | 92 |
| 91 add_time_segment(string, seconds, " second", " seconds", true); | 93 add_time_segment(string, seconds, " second", " seconds", true); |
| 92 return string.str(); | 94 return string.str(); |
| 93 } | 95 } |
| 94 | 96 |
| 95 int64_t GetSystemTime() { | 97 Timestamp GetSystemTime() { |
| 96 static_assert(sizeof(int64_t) >= sizeof(time_t)); | 98 return QDateTime::currentDateTime().toUTC().toSecsSinceEpoch(); |
| 97 time_t t = std::time(nullptr); | |
| 98 return static_cast<int64_t>(t); | |
| 99 } | 99 } |
| 100 | 100 |
| 101 } // namespace Time | 101 } // namespace Time |
