apply UTC offset during timestamp conversion
[libi2ncommon] / src / timefunc.cpp
index 805ffae..6fe2b98 100644 (file)
@@ -1110,7 +1110,8 @@ namespace clock {
         memcpy (&tmp_tm, &tm, sizeof (tmp_tm));
 
         errno = 0;
-        const time_t t = mktime (&tmp_tm);
+
+        time_t t = mktime (&tmp_tm);
         if (t == - 1) { /* Glibc does not set errno on out-of-range here! */
             const char *datestr = asctime (&tm);
             throw conversion_error (errno,
@@ -1119,6 +1120,14 @@ namespace clock {
                                     + "}");
         }
 
+        /*
+         * The timezone shenanigans are there to force we end up using UTC
+         * internally. The trouble with mktime(3) is that the unix timestamp
+         * is defined as “seconds since epoch” but “expressed as local time”.
+         */
+        t += tm.tm_gmtoff;
+        t -= timezone;
+
         tmp_time = Time (t, 0l, id, var);
 
         this->swap (tmp_time);