Mercurial > minori
changeset 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 | 8769c5d50b06 |
children | bf89fbf7ff38 |
files | include/core/time.h src/core/time.cc |
diffstat | 2 files changed, 11 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/include/core/time.h Sun May 19 18:25:14 2024 -0400 +++ b/include/core/time.h Tue Jun 11 04:24:19 2024 -0400 @@ -6,7 +6,7 @@ namespace Time { /* this is in SECONDS */ -using Timestamp = uint64_t; +using Timestamp = int64_t; enum class Units { Seconds, @@ -16,7 +16,8 @@ std::string GetSecondsAsRelativeString(Timestamp length); std::string GetSecondsAsAbsoluteString(Units unit_cutoff, Timestamp amount, double unit_in_seconds = 1.0); -int64_t GetSystemTime(); +/* in UTC */ +Timestamp GetSystemTime(); }; // namespace Time
--- a/src/core/time.cc Sun May 19 18:25:14 2024 -0400 +++ b/src/core/time.cc Tue Jun 11 04:24:19 2024 -0400 @@ -7,24 +7,26 @@ #include <ctime> #include <string> +#include <QDateTime> + namespace Time { -static int64_t GetSecondsInMinutes(Timestamp length) { +static Timestamp GetSecondsInMinutes(Timestamp length) { return std::llround(static_cast<double>(length) / 60.0); } -static int64_t GetSecondsInHours(Timestamp length) { +static Timestamp GetSecondsInHours(Timestamp length) { return std::llround(static_cast<double>(length) / 3600.0); } -static int64_t GetSecondsInDays(Timestamp length) { +static Timestamp GetSecondsInDays(Timestamp length) { return std::llround(static_cast<double>(length) / 86400.0); } std::string GetSecondsAsRelativeString(Timestamp length) { std::string result; - auto get = [](int64_t val, const std::string& s, const std::string& p) { + auto get = [](Timestamp val, const std::string& s, const std::string& p) { return Strings::ToUtf8String(val) + " " + (val == 1 ? s : p); }; @@ -92,10 +94,8 @@ return string.str(); } -int64_t GetSystemTime() { - static_assert(sizeof(int64_t) >= sizeof(time_t)); - time_t t = std::time(nullptr); - return static_cast<int64_t>(t); +Timestamp GetSystemTime() { + return QDateTime::currentDateTime().toUTC().toSecsSinceEpoch(); } } // namespace Time