chiark / gitweb /
silent a few more gcc warnings
[elogind.git] / src / shared / time-util.c
index 9ee711a49e58e0c1a7c276201897f52d0f305a36..7bb7d4ec5a50aadf378ddcab0002488b335e5f27 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <time.h>
 #include <string.h>
+#include <sys/timex.h>
 
 #include "util.h"
 #include "time-util.h"
@@ -168,6 +169,28 @@ char *format_timestamp(char *buf, size_t l, usec_t t) {
         return buf;
 }
 
+char *format_timestamp_us(char *buf, size_t l, usec_t t) {
+        struct tm tm;
+        time_t sec;
+
+        assert(buf);
+        assert(l > 0);
+
+        if (t <= 0)
+                return NULL;
+
+        sec = (time_t) (t / USEC_PER_SEC);
+        localtime_r(&sec, &tm);
+
+        if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0)
+                return NULL;
+        snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", t % USEC_PER_SEC);
+        if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
+                return NULL;
+
+        return buf;
+}
+
 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
         usec_t n, d;
 
@@ -260,7 +283,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
         /* The result of this function can be parsed with parse_sec */
 
         for (i = 0; i < ELEMENTSOF(table); i++) {
-                int k;
+                int k = 0;
                 size_t n;
                 bool done = false;
                 usec_t a, b;
@@ -770,3 +793,15 @@ int parse_nsec(const char *t, nsec_t *nsec) {
 
         return 0;
 }
+
+bool ntp_synced(void) {
+        struct timex txc = {};
+
+        if (adjtimex(&txc) < 0)
+                return false;
+
+        if (txc.status & STA_UNSYNC)
+                return false;
+
+        return true;
+}