diff options
| author | Eugen Wissner <belka@caraus.de> | 2024-06-30 20:46:40 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2024-06-30 20:46:40 +0200 |
| commit | 78f9939afc1bd24f5c929bfc8a643bbffedb35c5 (patch) | |
| tree | 8539c3dfe37e360389096e743f2f311c6afa431c /src/timedate.cpp | |
| parent | 8bf3a92cbc3aea9ae47177d877420f966bc9acf5 (diff) | |
| download | slack-timedate-78f9939afc1bd24f5c929bfc8a643bbffedb35c5.tar.gz | |
Fix setting the clock by one hour back
Diffstat (limited to 'src/timedate.cpp')
| -rw-r--r-- | src/timedate.cpp | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/src/timedate.cpp b/src/timedate.cpp index c7c2406..509bb95 100644 --- a/src/timedate.cpp +++ b/src/timedate.cpp @@ -161,9 +161,6 @@ static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio namespace dlackware::timedate { - constexpr const std::uint64_t usec_per_sec = 1000000ULL; - constexpr const std::uint64_t nsec_per_usec = 1000ULL; - timedate1::timedate1() : interface_vtable{ &slack_method_call, &slack_get_property } { @@ -279,30 +276,26 @@ namespace dlackware::timedate } } - void set_time(std::int64_t seconds_since_epoch, bool relative, bool) + void set_time(std::int64_t usec_utc, bool relative, bool) { timespec ts; + std::chrono::system_clock::duration current_time_since_epoch; if (relative) { - if (clock_gettime(CLOCK_REALTIME, &ts) == -1) - { - throw std::system_error(errno, std::generic_category()); - } - ts.tv_sec += static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec); - ts.tv_nsec += (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec; - - if (ts.tv_nsec < 0) - { - --ts.tv_sec; - ts.tv_nsec += dlackware::timedate::usec_per_sec; - } + current_time_since_epoch = std::chrono::system_clock::now().time_since_epoch(); + current_time_since_epoch += std::chrono::microseconds(usec_utc); } else { - ts.tv_sec = static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec); - ts.tv_nsec = (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec; + current_time_since_epoch = std::chrono::microseconds(usec_utc); } + auto seconds_since_epoch = std::chrono::floor<std::chrono::seconds>(current_time_since_epoch); + + ts.tv_sec = seconds_since_epoch.count(); + ts.tv_nsec = std::chrono::duration_cast<std::chrono::microseconds>( + current_time_since_epoch - seconds_since_epoch).count(); + if (clock_settime(CLOCK_REALTIME, &ts) == -1) { throw std::system_error(errno, std::generic_category()); |
