From d75053b2439ab30d45aaa55365e7586498cd3299 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 27 Jun 2024 01:24:37 +0200 Subject: [PATCH] Implement TimeUSec property --- data/org.freedesktop.timedate1.xml | 3 ++- src/timedate.cpp | 33 +++++++++++++++++++++++++++--- src/timedate.h | 5 ++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/data/org.freedesktop.timedate1.xml b/data/org.freedesktop.timedate1.xml index ab148da..effd806 100644 --- a/data/org.freedesktop.timedate1.xml +++ b/data/org.freedesktop.timedate1.xml @@ -7,7 +7,8 @@ - + + diff --git a/src/timedate.cpp b/src/timedate.cpp index 42bacf6..e6ec83f 100644 --- a/src/timedate.cpp +++ b/src/timedate.cpp @@ -125,9 +125,13 @@ static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr::create(dlackware::timedate::ntp_synchronized()); } - else if (prop_name == "TimeUSecRTC") + else if (prop_name == "TimeUSec") { - result = Glib::Variant::create(dlackware::timedate::time_usec_rtc()); + result = Glib::Variant::create(dlackware::timedate::time_usec()); + } + else if (prop_name == "RTCTimeUSec") + { + result = Glib::Variant::create(dlackware::timedate::rtc_time_usec()); } } @@ -338,7 +342,30 @@ namespace dlackware::timedate return ntp_result != TIME_ERROR && (buffer.status & STA_UNSYNC) == 0; } - std::uint64_t time_usec_rtc() + std::uint64_t time_usec() + { + timespec spec; + + if (clock_gettime(CLOCK_REALTIME, &spec) == -1) + { + throw std::system_error(errno, std::generic_category()); + } + time_t seconds = spec.tv_sec; + tm time; + + if (local_rtc()) + { + gmtime_r(&seconds, &time); + } + else + { + localtime_r(&seconds, &time); + } + return std::chrono::duration_cast( + std::chrono::seconds(std::mktime(&time)) + std::chrono::nanoseconds(spec.tv_nsec)).count(); + } + + std::uint64_t rtc_time_usec() { return std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); diff --git a/src/timedate.h b/src/timedate.h index 952209c..1c33594 100644 --- a/src/timedate.h +++ b/src/timedate.h @@ -44,7 +44,10 @@ namespace dlackware::timedate bool ntp_synchronized(); // Shows the current time in the RTC. - std::uint64_t time_usec_rtc(); + std::uint64_t rtc_time_usec(); + + // Shows the current time on the system. + std::uint64_t time_usec(); // Returns the timezones available on the system. std::vector list_timezones();