chiark / gitweb /
Cleaned up more unneeded functions and types in:
[elogind.git] / src / basic / time-util.c
index ecca227c74bb718cb2b1fc872200ad23b1b7b626..17d3da6fa296c5821300f2767907375f9dc201fb 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "util.h"
 #include "time-util.h"
+#include "path-util.h"
 #include "strv.h"
 
 usec_t now(clockid_t clock_id) {
@@ -36,6 +37,17 @@ usec_t now(clockid_t clock_id) {
         return timespec_load(&ts);
 }
 
+/// UNNEEDED by elogind
+#if 0
+nsec_t now_nsec(clockid_t clock_id) {
+        struct timespec ts;
+
+        assert_se(clock_gettime(clock_id, &ts) == 0);
+
+        return timespec_load_nsec(&ts);
+}
+#endif // 0
+
 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
         assert(ts);
 
@@ -67,6 +79,8 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
         return ts;
 }
 
+/// UNNEEDED by elogind
+#if 0
 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
         int64_t delta;
         assert(ts);
@@ -88,6 +102,32 @@ dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
         return ts;
 }
 
+dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
+        int64_t delta;
+
+        if (u == USEC_INFINITY) {
+                ts->realtime = ts->monotonic = USEC_INFINITY;
+                return ts;
+        }
+        ts->realtime = now(CLOCK_REALTIME);
+        ts->monotonic = now(CLOCK_MONOTONIC);
+
+        delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
+
+        if ((int64_t) ts->realtime > delta)
+                ts->realtime -= delta;
+        else
+                ts->realtime = 0;
+
+        if ((int64_t) ts->monotonic > delta)
+                ts->monotonic -= delta;
+        else
+                ts->monotonic = 0;
+
+        return ts;
+}
+#endif // 0
+
 usec_t timespec_load(const struct timespec *ts) {
         assert(ts);
 
@@ -103,6 +143,18 @@ usec_t timespec_load(const struct timespec *ts) {
                 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
 }
 
+nsec_t timespec_load_nsec(const struct timespec *ts) {
+        assert(ts);
+
+        if (ts->tv_sec == (time_t) -1 &&
+            ts->tv_nsec == (long) -1)
+                return NSEC_INFINITY;
+
+        return
+                (nsec_t) ts->tv_sec * NSEC_PER_SEC +
+                (nsec_t) ts->tv_nsec;
+}
+
 struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
         assert(ts);
 
@@ -209,9 +261,12 @@ char *format_timestamp_us(char *buf, size_t l, usec_t t) {
         return format_timestamp_internal_us(buf, l, t, false);
 }
 
+/// UNNEEDED by elogind
+#if 0
 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
         return format_timestamp_internal_us(buf, l, t, true);
 }
+#endif // 0
 
 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
         const char *s;
@@ -386,6 +441,8 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
         return buf;
 }
 
+/// UNNEEDED by elogind
+#if 0
 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
 
         assert(f);
@@ -418,8 +475,6 @@ int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
         return 0;
 }
 
-/// UNNEEDED by elogind
-#if 0
 int parse_timestamp(const char *t, usec_t *usec) {
         static const struct {
                 const char *name;
@@ -745,6 +800,8 @@ int parse_sec(const char *t, usec_t *usec) {
         return 0;
 }
 
+/// UNNEEDED by elogind
+#if 0
 int parse_nsec(const char *t, nsec_t *nsec) {
         static const struct {
                 const char *suffix;
@@ -870,8 +927,6 @@ int parse_nsec(const char *t, nsec_t *nsec) {
         return 0;
 }
 
-/// UNNEEDED by elogind
-#if 0
 bool ntp_synced(void) {
         struct timex txc = {};
 
@@ -953,7 +1008,10 @@ bool timezone_is_valid(const char *name) {
         const char *p, *t;
         struct stat st;
 
-        if (!name || *name == 0 || *name == '/')
+        if (isempty(name))
+                return false;
+
+        if (name[0] == '/')
                 return false;
 
         for (p = name; *p; p++) {
@@ -985,7 +1043,6 @@ bool timezone_is_valid(const char *name) {
 
         return true;
 }
-#endif // 0
 
 clockid_t clock_boottime_or_monotonic(void) {
         static clockid_t clock = -1;
@@ -1004,3 +1061,31 @@ clockid_t clock_boottime_or_monotonic(void) {
 
         return clock;
 }
+
+int get_timezone(char **tz) {
+        _cleanup_free_ char *t = NULL;
+        const char *e;
+        char *z;
+        int r;
+
+        r = readlink_malloc("/etc/localtime", &t);
+        if (r < 0)
+                return r; /* returns EINVAL if not a symlink */
+
+        e = path_startswith(t, "/usr/share/zoneinfo/");
+        if (!e)
+                e = path_startswith(t, "../usr/share/zoneinfo/");
+        if (!e)
+                return -EINVAL;
+
+        if (!timezone_is_valid(e))
+                return -EINVAL;
+
+        z = strdup(e);
+        if (!z)
+                return -ENOMEM;
+
+        *tz = z;
+        return 0;
+}
+#endif // 0