[PATCHv2] util: fix virTimeLocalOffsetFromUTC DST processing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The original version of virTimeLocalOffsetFromUTC() would fail for
certain times of the day if daylight savings time was active. This
could most easily be seen by uncommenting the TEST_LOCALOFFSET() cases
that include a DST setting.

After a lot of experimenting, I found that the way to solve it in
almost all test cases is to set tm_isdst = -1 in the stuct tm prior to
calling mktime(). Once this is done, the correct offset is returned
for all test cases at all times except the two hours just after
00:00:00 Jan 1 UTC - during that time, any timezone that is *behind*
UTC, and that is supposed to always be in DST will not have DST
accounted for in its offset.

I believe that the code of virTimeLocalOffsetFromUTC() is actually
correct for all cases, but the problem still encountered is due to our
inability to come up with a TZ string that properly forces DST to
*always* be active. Since a modfication of the (currently fixed)
expected result data to account for this would necessarily use the same
functions that we're trying to test, I've instead just made the test
program conditionally bypass the problematic cases if the current date
is either December 31 or January 1. This way we get maximum testing
during 363 days of the year, but don't get false failures on Dec 31
and Jan 1.
---

Change from V1: correct typo in commit message pointed out by Eric,
and also followed his suggestion to enable the sometimes-failing tests
unless the current localtime is Jan 1 or Dec 31.

 src/util/virtime.c  |  3 +++
 tests/virtimetest.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/src/util/virtime.c b/src/util/virtime.c
index 3a56400..c69dff1 100644
--- a/src/util/virtime.c
+++ b/src/util/virtime.c
@@ -377,6 +377,9 @@ virTimeLocalOffsetFromUTC(long *offset)
         return -1;
     }
 
+    /* tell mktime to figure out itself whether or not DST is in effect */
+    gmtimeinfo.tm_isdst = -1;
+
     /* mktime() also obeys current timezone rules */
     if ((utc = mktime(&gmtimeinfo)) == (time_t)-1) {
         virReportSystemError(errno, "%s",
diff --git a/tests/virtimetest.c b/tests/virtimetest.c
index bf27682..f378076 100644
--- a/tests/virtimetest.c
+++ b/tests/virtimetest.c
@@ -101,6 +101,28 @@ testTimeLocalOffset(const void *args)
     return 0;
 }
 
+
+/* return true if the date is Jan 1 or Dec 31 (localtime) */
+static bool
+IsJan1OrDec31(void)
+{
+    time_t current = time(NULL);
+    struct tm timeinfo;
+
+    if (current == (time_t)-1) {
+        VIR_DEBUG("time() failed");
+        return false;
+    }
+    if (!localtime_r(&current, &timeinfo)) {
+        VIR_DEBUG("localtime_r() failed");
+        return false;
+    }
+
+    return (timeinfo.tm_mon == 0 && timeinfo.tm_mday == 1) ||
+            (timeinfo.tm_mon == 11 && timeinfo.tm_mday == 31);
+}
+
+
 static int
 mymain(void)
 {
@@ -161,21 +183,44 @@ mymain(void)
 
     TEST_LOCALOFFSET("VIR00:30", -30 * 60);
     TEST_LOCALOFFSET("VIR01:30", -90 * 60);
+    TEST_LOCALOFFSET("VIR05:00", (-5 * 60) * 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
+    TEST_LOCALOFFSET("VIR-00:30VID,0/00:00:00,366/23:59:59",
+                     ((1 * 60) + 30) * 60);
+    TEST_LOCALOFFSET("VIR-02:30VID,0/00:00:00,366/23:59:59",
+                     ((3 * 60) + 30) * 60);
+    TEST_LOCALOFFSET("VIR-02:30VID-04:30,0/00:00:00,366/23:59:59",
+                     ((4 * 60) + 30) * 60);
+    TEST_LOCALOFFSET("VIR-12:00VID-13:00,0/00:00:00,366/23:59:59",
+                     ((13 * 60) +  0) * 60);
+
+    if (!IsJan1OrDec31()) {
+        /* experiments have shown that the following tests will fail
+         * during certain hours of Dec 31 or Jan 1 (depending on the
+         * TZ setting in the shell running the test, but in general
+         * for a period that apparently starts at 00:00:00 UTC Jan 1
+         * and continues for 1 - 2 hours). We've determined this is
+         * due to our inability to specify a timezone with DST on/off
+         * settings that make it truly *always* on DST - i.e. it is a
+         * failing of the test data, *not* of the function we are
+         * testing. So to test as much as possible, we still run these
+         * tests, except on Dec 31 and Jan 1.
+         */
+
+        TEST_LOCALOFFSET("VIR02:45VID00:45,0/00:00:00,366/23:59:59",
+                         -45 * 60);
+        TEST_LOCALOFFSET("VIR05:00VID04:00,0/00:00:00,366/23:59:59",
+                         ((-4 * 60) +  0) * 60);
+        TEST_LOCALOFFSET("VIR11:00VID10:00,0/00:00:00,366/23:59:59",
+                         ((-10 * 60) +  0) * 60);
+    }
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]