This commit is contained in:
parent
6fc7f86a89
commit
cbbefae284
@ -6,6 +6,7 @@
|
||||
<property name="LocalRTC" type="b" access="read"/>
|
||||
<property name="NTP" type="b" access="read"/>
|
||||
<property name="CanNTP" type="b" access="read"/>
|
||||
<property name="NTPSynchronized" type="b" access="read"/>
|
||||
<method name="SetTime">
|
||||
<arg name="usec_utc" type="x" direction="in"/>
|
||||
<arg name="relative" type="b" direction="in"/>
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <sys/timex.h>
|
||||
#include "timedate.h"
|
||||
|
||||
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());
|
||||
}
|
||||
else if (prop_name == "NTPSynchronized")
|
||||
{
|
||||
result = Glib::Variant<bool>::create(dlackware::timedate::ntp_synchronized());
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
std::ifstream fh{ "/etc/hardwareclock", std::ios::in };
|
||||
@ -288,33 +322,15 @@ namespace dlackware::timedate
|
||||
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 (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)
|
||||
if (ntp_result == -1)
|
||||
{
|
||||
throw std::system_error(errno, std::generic_category());
|
||||
}
|
||||
return ntp_result != TIME_ERROR && (buffer.status & STA_UNSYNC) == 0;
|
||||
}
|
||||
}
|
||||
|
@ -40,6 +40,9 @@ namespace dlackware::timedate
|
||||
// Returns whether NTP is available.
|
||||
bool can_ntp();
|
||||
|
||||
// Shows whether the kernel reports the time as synchronized.
|
||||
bool ntp_synchronized();
|
||||
|
||||
// Returns the timezones available on the system.
|
||||
std::vector<Glib::ustring> list_timezones();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user