Implement TimeUSec property
All checks were successful
Build / build (push) Successful in 16s

This commit is contained in:
Eugen Wissner 2024-06-27 01:24:37 +02:00
parent b11e18e560
commit d75053b243
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 36 additions and 5 deletions

View File

@ -7,7 +7,8 @@
<property name="NTP" type="b" access="read"/> <property name="NTP" type="b" access="read"/>
<property name="CanNTP" type="b" access="read"/> <property name="CanNTP" type="b" access="read"/>
<property name="NTPSynchronized" type="b" access="read"/> <property name="NTPSynchronized" type="b" access="read"/>
<property name="TimeUSecRTC" type="t" access="read"/> <property name="TimeUSec" type="t" access="read"/>
<property name="RTCTimeUSec" type="t" access="read"/>
<method name="SetTime"> <method name="SetTime">
<arg name="usec_utc" type="x" direction="in"/> <arg name="usec_utc" type="x" direction="in"/>
<arg name="relative" type="b" direction="in"/> <arg name="relative" type="b" direction="in"/>

View File

@ -125,9 +125,13 @@ static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio
{ {
result = Glib::Variant<bool>::create(dlackware::timedate::ntp_synchronized()); result = Glib::Variant<bool>::create(dlackware::timedate::ntp_synchronized());
} }
else if (prop_name == "TimeUSecRTC") else if (prop_name == "TimeUSec")
{ {
result = Glib::Variant<std::uint64_t>::create(dlackware::timedate::time_usec_rtc()); result = Glib::Variant<std::uint64_t>::create(dlackware::timedate::time_usec());
}
else if (prop_name == "RTCTimeUSec")
{
result = Glib::Variant<std::uint64_t>::create(dlackware::timedate::rtc_time_usec());
} }
} }
@ -338,7 +342,30 @@ namespace dlackware::timedate
return ntp_result != TIME_ERROR && (buffer.status & STA_UNSYNC) == 0; 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::microseconds>(
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::microseconds>( return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count(); std::chrono::system_clock::now().time_since_epoch()).count();

View File

@ -44,7 +44,10 @@ namespace dlackware::timedate
bool ntp_synchronized(); bool ntp_synchronized();
// Shows the current time in the RTC. // 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. // Returns the timezones available on the system.
std::vector<Glib::ustring> list_timezones(); std::vector<Glib::ustring> list_timezones();