summaryrefslogtreecommitdiff
path: root/src/libstddjb/sysclock_set.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
commit3534b428629be185e096be99e3bd5fdfe32d5544 (patch)
tree210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/sysclock_set.c
downloadskalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/sysclock_set.c')
-rw-r--r--src/libstddjb/sysclock_set.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libstddjb/sysclock_set.c b/src/libstddjb/sysclock_set.c
new file mode 100644
index 0000000..0ec51c6
--- /dev/null
+++ b/src/libstddjb/sysclock_set.c
@@ -0,0 +1,53 @@
+/* ISC license. */
+
+/* MT-unsafe */
+
+#include <skalibs/config.h>
+#include <skalibs/sysdeps.h>
+
+#ifdef SKALIBS_FLAG_USERT
+# ifndef SKALIBS_HASCLOCKRT
+# undef SKALIBS_FLAG_USERT
+# warning "SKALIBS_FLAG_USERT set but SKALIBS_HASCLOCKRT not found. Clearing SKALIBS_FLAG_USERT."
+# endif
+#endif
+
+#ifndef SKALIBS_FLAG_USERT
+# ifndef SKALIBS_HASSETTIMEOFDAY
+# error "SKALIBS_FLAG_USERT clear but SKALIBS_HASSETTIMEOFDAY not found. How do your set your system clock?"
+# endif
+#endif
+
+
+#ifdef SKALIBS_FLAG_USERT
+
+#include <time.h>
+#include <skalibs/tai.h>
+
+int sysclock_set (tain_t const *a)
+{
+ struct timespec now ;
+ tain_t aa ;
+ tain_add(&aa, a, &tain_nano500) ;
+ if (!timespec_from_tain(&now, &aa)) return 0 ;
+ if (clock_settime(CLOCK_REALTIME, &now) < 0) return 0 ;
+ return 1 ;
+}
+
+#else
+
+#include <skalibs/nonposix.h>
+#include <sys/time.h>
+#include <skalibs/tai.h>
+
+int sysclock_set (tain_t const *a)
+{
+ struct timeval now ;
+ tain_t aa ;
+ tain_add(&aa, a, &tain_nano500) ;
+ if (!timeval_from_tain(&now, &aa)) return 0 ;
+ if (settimeofday(&now, 0) < 0) return 0 ;
+ return 1 ;
+}
+
+#endif