]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: new function virTimeLocalOffsetFromUTC
authorLaine Stump <laine@laine.org>
Sat, 24 May 2014 14:21:26 +0000 (08:21 -0600)
committerLaine Stump <laine@laine.org>
Mon, 26 May 2014 10:51:05 +0000 (13:51 +0300)
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.

The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.

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; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.

(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)

Signed-off-by: Eric Blake <eblake@redhat.com>
src/libvirt_private.syms
src/util/virtime.c
src/util/virtime.h
tests/virtimetest.c

index c3332c9b2cfebd1260d435487bc5cc2792cbb076..cb635cd5e0bb67a75079c15b0a92664d5c255d6a 100644 (file)
@@ -1984,6 +1984,7 @@ virTimeFieldsNow;
 virTimeFieldsNowRaw;
 virTimeFieldsThen;
 virTimeFieldsThenRaw;
+virTimeLocalOffsetFromUTC;
 virTimeMillisNow;
 virTimeMillisNowRaw;
 virTimeStringNow;
index caa4e241f62e88338f4d9694df90becba528bafe..3a56400fe2b791b62ad6a0f34848fc7c24925b8d 100644 (file)
@@ -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,46 @@ 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 be set to the difference
+ *          between localtime and UTC in seconds (east of UTC is a
+ *          positive number, and west of UTC is a negative number.
+ *
+ * Returns 0 on success, -1 on error with error reported
+ */
+int
+virTimeLocalOffsetFromUTC(long *offset)
+{
+    struct tm gmtimeinfo;
+    time_t current, utc;
+
+    /* time() gives seconds since Epoch in current timezone */
+    if ((current = time(NULL)) == (time_t)-1) {
+        virReportSystemError(errno, "%s",
+                             _("failed to get current system time"));
+        return -1;
+    }
+
+    /* treat current as if it were in UTC */
+    if (!gmtime_r(&current, &gmtimeinfo)) {
+        virReportSystemError(errno, "%s",
+                             _("gmtime_r failed"));
+        return -1;
+    }
+
+    /* mktime() also obeys current timezone rules */
+    if ((utc = mktime(&gmtimeinfo)) == (time_t)-1) {
+        virReportSystemError(errno, "%s",
+                             _("mktime failed"));
+        return -1;
+    }
+
+    *offset = current - utc;
+    return 0;
+}
index a5bd6526024e11642e4e661298fa294404b594f9..25332dba93ab83191fecf0f70f776a17af469d58 100644 (file)
@@ -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(long *offset)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 
 #endif
index 73a3c3ed5cfab62fb2ecb453b4d4e564716a9401..bf2768253345b3c7cb7032d8aea94223826a5392 100644 (file)
@@ -72,6 +72,35 @@ static int testTimeFields(const void *args)
 }
 
 
+typedef struct {
+    const char *zone;
+    long offset;
+} testTimeLocalOffsetData;
+
+static int
+testTimeLocalOffset(const void *args)
+{
+    const testTimeLocalOffsetData *data = args;
+    long 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 %ld got %ld\n",
+                  data->offset, actual);
+        return -1;
+    }
+    return 0;
+}
+
 static int
 mymain(void)
 {
@@ -119,6 +148,35 @@ 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("VIR01:30", -90 * 60);
+    TEST_LOCALOFFSET("UTC", 0);
+    TEST_LOCALOFFSET("VIR-00:30", 30 * 60);
+    TEST_LOCALOFFSET("VIR-01:30", 90 * 60);
+#if __TEST_DST
+    /* test DST processing with timezones that always
+     * have DST in effect; what's more, cover a zone with
+     * with an unusual DST different than a usual one hour
+     */
+    /* NB: These tests fail at certain times of the day, so
+     * must be disabled until we figure out why
+     */
+    TEST_LOCALOFFSET("VIR-00:30VID,0,365", 90 * 60);
+    TEST_LOCALOFFSET("VIR-02:30VID,0,365", 210 * 60);
+    TEST_LOCALOFFSET("VIR-02:30VID-04:30,0,365", 270 * 60);
+#endif
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }