Fix setting the clock by one hour back
All checks were successful
Build / build (push) Successful in 17s

This commit is contained in:
Eugen Wissner 2024-06-30 20:46:40 +02:00
parent 8bf3a92cbc
commit 78f9939afc
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 12 additions and 19 deletions

View File

@ -161,9 +161,6 @@ static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio
namespace dlackware::timedate namespace dlackware::timedate
{ {
constexpr const std::uint64_t usec_per_sec = 1000000ULL;
constexpr const std::uint64_t nsec_per_usec = 1000ULL;
timedate1::timedate1() timedate1::timedate1()
: interface_vtable{ &slack_method_call, &slack_get_property } : interface_vtable{ &slack_method_call, &slack_get_property }
{ {
@ -279,30 +276,26 @@ namespace dlackware::timedate
} }
} }
void set_time(std::int64_t seconds_since_epoch, bool relative, bool) void set_time(std::int64_t usec_utc, bool relative, bool)
{ {
timespec ts; timespec ts;
std::chrono::system_clock::duration current_time_since_epoch;
if (relative) if (relative)
{ {
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) current_time_since_epoch = std::chrono::system_clock::now().time_since_epoch();
{ current_time_since_epoch += std::chrono::microseconds(usec_utc);
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 else
{ {
ts.tv_sec = static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec); current_time_since_epoch = std::chrono::microseconds(usec_utc);
ts.tv_nsec = (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec;
} }
auto seconds_since_epoch = std::chrono::floor<std::chrono::seconds>(current_time_since_epoch);
ts.tv_sec = seconds_since_epoch.count();
ts.tv_nsec = std::chrono::duration_cast<std::chrono::microseconds>(
current_time_since_epoch - seconds_since_epoch).count();
if (clock_settime(CLOCK_REALTIME, &ts) == -1) if (clock_settime(CLOCK_REALTIME, &ts) == -1)
{ {
throw std::system_error(errno, std::generic_category()); throw std::system_error(errno, std::generic_category());

View File

@ -63,7 +63,7 @@ namespace dlackware::timedate
// Changes the date/time // Changes the date/time
// Takes the amount of seconds since UNIX epoche and // Takes the amount of seconds since UNIX epoche and
// Throws std::system_error. // 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. // Controls whether the RTC is local time or UTC.
void set_local_rtc(bool local_rtc, bool fix_system, bool user_interaction); void set_local_rtc(bool local_rtc, bool fix_system, bool user_interaction);