[libvirt] [PATCHv2 1/4] util: new function virTimeLocalOffsetFromUTC

Laine Stump laine at laine.org
Thu May 22 11:07:27 UTC 2014


Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current UTC, then
converting that into a struct tm with localtime_r(), then back to a
time_t using mktime; it again does the same operation, but using
gmtime_r() instead (for UTC). It then subtracts utc time from the
localtime, and finally adjusts if dst is set in the localtime timeinfo
(because for some reason mktime doesn't take that into account).

This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage.
---

Change from V1: add test cases with TZ set to different values (if
someone knows how to force DST on/off, I would gladly add some test
cases for this as well).

 src/libvirt_private.syms |  1 +
 src/util/virtime.c       | 41 ++++++++++++++++++++++++++++++++++++++++-
 src/util/virtime.h       |  5 +++--
 tests/virtimetest.c      | 44 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 88 insertions(+), 3 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c3332c9..cb635cd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1984,6 +1984,7 @@ virTimeFieldsNow;
 virTimeFieldsNowRaw;
 virTimeFieldsThen;
 virTimeFieldsThenRaw;
+virTimeLocalOffsetFromUTC;
 virTimeMillisNow;
 virTimeMillisNowRaw;
 virTimeStringNow;
diff --git a/src/util/virtime.c b/src/util/virtime.c
index caa4e24..c487eba 100644
--- a/src/util/virtime.c
+++ b/src/util/virtime.c
@@ -1,7 +1,7 @@
 /*
  * virtime.c: Time handling functions
  *
- * Copyright (C) 2006-2012 Red Hat, Inc.
+ * Copyright (C) 2006-2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -344,3 +344,42 @@ char *virTimeStringThen(unsigned long long when)
 
     return ret;
 }
+
+/**
+ * virTimeLocalOffsetFromUTC:
+ *
+ * This function is threadsafe, but is *not* async signal safe (due to
+ * localtime_r()).
+ *
+ * @offset: pointer to time_t that will difference between localtime
+ *          and UTC in seconds.
+ *
+ * Returns 0 on success, -1 on error with error reported
+ */
+int
+virTimeLocalOffsetFromUTC(time_t *offset)
+{
+    struct tm gmtimeinfo, localtimeinfo;
+    time_t current, utc, local;
+
+    if ((current = time(NULL)) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("failed to get current system time"));
+        return -1;
+    }
+
+    localtime_r(&current, &localtimeinfo);
+    gmtime_r(&current, &gmtimeinfo);
+
+    if ((local = mktime(&localtimeinfo)) < 0 ||
+        (utc = mktime(&gmtimeinfo)) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("mktime failed"));
+        return -1;
+    }
+
+    *offset = local - utc;
+    if (localtimeinfo.tm_isdst)
+        *offset += 3600;
+    return 0;
+}
diff --git a/src/util/virtime.h b/src/util/virtime.h
index a5bd652..cda9707 100644
--- a/src/util/virtime.h
+++ b/src/util/virtime.h
@@ -1,7 +1,7 @@
 /*
  * virtime.h: Time handling functions
  *
- * Copyright (C) 2006-2011 Red Hat, Inc.
+ * Copyright (C) 2006-2011, 2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -50,7 +50,6 @@ int virTimeStringNowRaw(char *buf)
 int virTimeStringThenRaw(unsigned long long when, char *buf)
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
-
 /* These APIs are *not* async signal safe and return -1,
  * raising a libvirt error on failure
  */
@@ -63,5 +62,7 @@ int virTimeFieldsThen(unsigned long long when, struct tm *fields)
 char *virTimeStringNow(void);
 char *virTimeStringThen(unsigned long long when);
 
+int virTimeLocalOffsetFromUTC(time_t *offset)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 
 #endif
diff --git a/tests/virtimetest.c b/tests/virtimetest.c
index 73a3c3e..d1a409f 100644
--- a/tests/virtimetest.c
+++ b/tests/virtimetest.c
@@ -72,6 +72,35 @@ static int testTimeFields(const void *args)
 }
 
 
+typedef struct {
+    const char *zone;
+    time_t offset;
+} testTimeLocalOffsetData;
+
+static int
+testTimeLocalOffset(const void *args)
+{
+    const testTimeLocalOffsetData *data = args;
+    time_t actual;
+
+    if (setenv("TZ", data->zone, 1) < 0) {
+        perror("setenv");
+        return -1;
+    }
+    tzset();
+
+    if (virTimeLocalOffsetFromUTC(&actual) < 0) {
+        return -1;
+    }
+
+    if (data->offset != actual) {
+        VIR_DEBUG("Expect Offset %d got %d\n",
+                  (int) data->offset, (int) actual);
+        return -1;
+    }
+    return 0;
+}
+
 static int
 mymain(void)
 {
@@ -119,6 +148,21 @@ mymain(void)
 
     TEST_FIELDS(2147483648000ull, 2038,  1, 19,  3, 14,  8);
 
+#define TEST_LOCALOFFSET(tz, off)       \
+    do {                                \
+       testTimeLocalOffsetData data = { \
+           .zone =  tz,                 \
+           .offset = off,               \
+        };                              \
+        if (virtTestRun("Test localtime offset for " #tz, \
+                         testTimeLocalOffset, &data) < 0) \
+            ret = -1;                   \
+    } while (0)
+
+    TEST_LOCALOFFSET("VIR00:30", -30 * 60);
+    TEST_LOCALOFFSET("UTC", 0);
+    TEST_LOCALOFFSET("VIR-00:30", 30 * 60);
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-- 
1.9.0




More information about the libvir-list mailing list