Add the NTPSynchronized property
Some checks failed
Build / build (push) Failing after 19s

This commit is contained in:
Eugen Wissner 2024-06-21 16:07:28 +02:00
parent 6fc7f86a89
commit cbbefae284
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 44 additions and 24 deletions

View File

@ -6,6 +6,7 @@
<property name="LocalRTC" type="b" access="read"/> <property name="LocalRTC" type="b" access="read"/>
<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"/>
<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

@ -20,7 +20,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <sys/timex.h>
#include "timedate.h" #include "timedate.h"
static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& sender, static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& sender,
@ -121,6 +121,10 @@ static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio
{ {
result = Glib::Variant<bool>::create(dlackware::timedate::can_ntp()); result = Glib::Variant<bool>::create(dlackware::timedate::can_ntp());
} }
else if (prop_name == "NTPSynchronized")
{
result = Glib::Variant<bool>::create(dlackware::timedate::ntp_synchronized());
}
} }
namespace dlackware::timedate namespace dlackware::timedate
@ -243,6 +247,36 @@ namespace dlackware::timedate
} }
} }
void set_time(gint64 seconds_since_epoch, bool relative, bool)
{
timespec ts;
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;
}
}
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;
}
if (clock_settime(CLOCK_REALTIME, &ts) == -1)
{
throw std::system_error(errno, std::generic_category());
}
}
bool local_rtc() bool local_rtc()
{ {
std::ifstream fh{ "/etc/hardwareclock", std::ios::in }; std::ifstream fh{ "/etc/hardwareclock", std::ios::in };
@ -288,33 +322,15 @@ namespace dlackware::timedate
return wait_status == 0 && standard_output == "bundle\n"; return wait_status == 0 && standard_output == "bundle\n";
} }
void set_time(gint64 seconds_since_epoch, bool relative, bool) bool ntp_synchronized()
{ {
timespec ts; timex buffer{ .modes = 0 };
int ntp_result = ntp_adjtime(&buffer);
if (relative) if (ntp_result == -1)
{
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;
}
}
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;
}
if (clock_settime(CLOCK_REALTIME, &ts) == -1)
{ {
throw std::system_error(errno, std::generic_category()); throw std::system_error(errno, std::generic_category());
} }
return ntp_result != TIME_ERROR && (buffer.status & STA_UNSYNC) == 0;
} }
} }

View File

@ -40,6 +40,9 @@ namespace dlackware::timedate
// Returns whether NTP is available. // Returns whether NTP is available.
bool can_ntp(); bool can_ntp();
// Shows whether the kernel reports the time as synchronized.
bool ntp_synchronized();
// 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();