From 78f9939afc1bd24f5c929bfc8a643bbffedb35c5 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 30 Jun 2024 20:46:40 +0200 Subject: [PATCH] Fix setting the clock by one hour back --- src/timedate.cpp | 29 +++++++++++------------------ src/timedate.h | 2 +- 2 files changed, 12 insertions(+), 19 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(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(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(current_time_since_epoch); + + ts.tv_sec = seconds_since_epoch.count(); + ts.tv_nsec = std::chrono::duration_cast( + current_time_since_epoch - seconds_since_epoch).count(); + if (clock_settime(CLOCK_REALTIME, &ts) == -1) { throw std::system_error(errno, std::generic_category()); diff --git a/src/timedate.h b/src/timedate.h index a1b2852..e197b4b 100644 --- a/src/timedate.h +++ b/src/timedate.h @@ -63,7 +63,7 @@ namespace dlackware::timedate // Changes the date/time // Takes the amount of seconds since UNIX epoche and // Throws std::system_error. - void set_time(std::int64_t seconds_since_epoch, bool relative, bool user_interaction); + void set_time(std::int64_t usec_utc, bool relative, bool user_interaction); // Controls whether the RTC is local time or UTC. void set_local_rtc(bool local_rtc, bool fix_system, bool user_interaction);