summaryrefslogtreecommitdiff
path: root/slack-timedate.cpp
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2024-04-14 10:49:28 +0200
committerEugen Wissner <belka@caraus.de>2024-04-14 10:49:28 +0200
commit5f35df67c2c1685c932453ea27518dcf729c859a (patch)
tree040b5e09782cd4241c2fffd896e6734e78f5cbdf /slack-timedate.cpp
parent729e175e6b96099323a4e3266faad9af0958112e (diff)
downloadslack-timedate-5f35df67c2c1685c932453ea27518dcf729c859a.tar.gz
Support relative time in SetTime
Diffstat (limited to 'slack-timedate.cpp')
-rw-r--r--slack-timedate.cpp76
1 files changed, 35 insertions, 41 deletions
diff --git a/slack-timedate.cpp b/slack-timedate.cpp
index bde70af..9c07ebe 100644
--- a/slack-timedate.cpp
+++ b/slack-timedate.cpp
@@ -16,7 +16,6 @@
*
*/
-#include <gio/gio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -30,9 +29,6 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
gboolean user_interaction, relative, is_localtime, use_ntp;
gint64 usec_utc;
- FILE *fp = fopen("/tmp/settime", "w");
- fprintf(fp, "Method %s\n", method_name);
-
// Set time zone
if (g_strcmp0 (method_name, "SetTimezone") == 0) {
g_variant_get (parameters, "(&sb)", &timezone, &user_interaction);
@@ -48,16 +44,17 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
} else if (g_strcmp0 (method_name, "SetTime") == 0) {
g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction);
- fprintf(fp, "Seconds since epoche %lld %llu\n", usec_utc, relative);
-
// Set time
//if (!slack_set_time (usec_utc, slack_get_is_localtime ())) {
- if (slack_set_time (usec_utc)) g_dbus_method_invocation_return_value (invocation,
- NULL);
- else g_dbus_method_invocation_return_error (invocation,
- G_IO_ERROR,
- G_IO_ERROR_FAILED,
- "Failed to set system clock");
+ if (slack_set_time(usec_utc, relative))
+ {
+ g_dbus_method_invocation_return_value(invocation, NULL);
+ }
+ else
+ {
+ g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Failed to set system clock");
+ }
} else if (g_strcmp0 (method_name, "SetNTP") == 0) {
g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction);
@@ -69,8 +66,7 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
G_IO_ERROR_FAILED,
"Error enabling NTP");
}
- fclose(fp);
- return ;
+ return;
}
static GVariant *slack_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *prop_name, GError **slack_err, gpointer user_data) {
@@ -194,33 +190,31 @@ gboolean slack_set_timezone (gchar *zone) {
return TRUE;
}
-gboolean slack_set_time (gint64 seconds_since_epoch) {
- struct timespec ts;
-/* gint exit_status;
- gboolean spawn_status;
- gchar *cmd;*/
-
- // Set system clock
- ts.tv_sec = (time_t) (seconds_since_epoch / USEC_PER_SEC);
- ts.tv_nsec = 0;
- if (clock_settime (CLOCK_REALTIME, &ts)) {
- return FALSE;
- }
-
- // Don't save the system time to the hardware clock. The saving takes much
- // time. Anyway it is saved automatically on shutdown.
- /* Set hardware clock
- if (!g_file_test ("/sbin/hwclock", G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE)) {
- return FALSE;
- }
- cmd = g_strdup_printf ("/sbin/hwclock %s --systohc", is_localtime ? "--localtime" : "-u");
- spawn_status = g_spawn_command_line_sync (cmd, NULL, NULL, &exit_status, NULL);
- g_free (cmd);
- if ((WEXITSTATUS (exit_status) != 0) || !spawn_status) {
- return FALSE;
- }*/
-
- return TRUE;
+gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative)
+{
+ timespec ts;
+
+ if (relative)
+ {
+ if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
+ {
+ return FALSE;
+ }
+ ts.tv_sec += static_cast<time_t>(seconds_since_epoch / USEC_PER_SEC);
+ ts.tv_nsec += (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC;
+
+ if (ts.tv_nsec < 0)
+ {
+ --ts.tv_sec;
+ ts.tv_nsec += USEC_PER_SEC;
+ }
+ }
+ else
+ {
+ ts.tv_sec = static_cast<time_t>(seconds_since_epoch / USEC_PER_SEC);
+ ts.tv_nsec = (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC;
+ }
+ return clock_settime (CLOCK_REALTIME, &ts) == 0;
}
gboolean slack_get_is_localtime () {