Fix incorrect LocalTime strings when timezone is behind UTC and not an integer number... v1.6.10
authorCarl Hetherington <cth@carlh.net>
Fri, 8 Nov 2019 22:48:42 +0000 (23:48 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 8 Nov 2019 22:50:03 +0000 (23:50 +0100)
src/local_time.cc
src/local_time.h
test/local_time_test.cc

index 401bd9d749d6ba6a593078c8082a832fbbc4494d..5030500efdc8b7a255b0eb618854e2440bf16d88 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
@@ -81,7 +81,10 @@ LocalTime::LocalTime (boost::posix_time::ptime t)
        set_local_time_zone ();
 }
 
-/** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset */
+/** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset.
+ *  @param tz_minute Offset from UTC in minutes; if the timezone is behind UTC this may be negative,
+ *  e.g. -04:30 would have tz_hour=-1 and tz_minute=-30.
+ */
 LocalTime::LocalTime (boost::posix_time::ptime t, int tz_hour, int tz_minute)
 {
        _year = t.date().year ();
@@ -163,6 +166,7 @@ LocalTime::LocalTime (string s)
 
        if (with_tz && s[tz_pos] == '-') {
                _tz_hour = -_tz_hour;
+               _tz_minute = -_tz_minute;
        }
 }
 
@@ -174,7 +178,7 @@ LocalTime::as_string (bool with_millisecond) const
        snprintf (
                buffer, sizeof (buffer),
                "%sT%s%s%02d:%02d",
-               date().c_str(), time_of_day(true, with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
+               date().c_str(), time_of_day(true, with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), abs(_tz_minute)
                );
        return buffer;
 }
index ad55ad5a8ee372d959d31019db76804c44e1920e..20658eb4d92b1caf7d92ce32d984e994318027d5 100644 (file)
@@ -84,8 +84,11 @@ private:
        int _second; ///< second number of the minute (0-59)
        int _millisecond; ///< millisecond number of the second (0-999)
 
-       int _tz_hour;   ///< hours by which this time is offset from UTC
-       int _tz_minute; ///< minutes by which this time is offset from UTC
+       int _tz_hour; ///< hours by which this time is offset from UTC; can be negative
+       /** Minutes by which this time is offset from UTC; if _tz_hour is negative
+        *  this will be either 0 or negative.
+        */
+       int _tz_minute;
 };
 
 std::ostream&
index 5b954e2fc1568ffd759b37ba9cb93e53573fb95c..b43b1cf3d95fae15e67dd8007c84fd9b7811b31d 100644 (file)
@@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE (local_time_test)
                BOOST_CHECK_EQUAL (t._minute, 6);
                BOOST_CHECK_EQUAL (t._second, 59);
                BOOST_CHECK_EQUAL (t._tz_hour, -9);
-               BOOST_CHECK_EQUAL (t._tz_minute, 30);
+               BOOST_CHECK_EQUAL (t._tz_minute, -30);
                BOOST_CHECK_EQUAL (t.as_string(), "2011-11-20T01:06:59-09:30");
        }
 
@@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE (local_time_test)
                BOOST_CHECK_EQUAL (t._second, 59);
                BOOST_CHECK_EQUAL (t._millisecond, 456);
                BOOST_CHECK_EQUAL (t._tz_hour, -9);
-               BOOST_CHECK_EQUAL (t._tz_minute, 30);
+               BOOST_CHECK_EQUAL (t._tz_minute, -30);
                BOOST_CHECK_EQUAL (t.as_string(true), "2011-11-20T01:06:59.456-09:30");
        }
 
@@ -109,4 +109,20 @@ BOOST_AUTO_TEST_CASE (local_time_test)
                BOOST_CHECK_EQUAL (b._tz_hour, 0);
                BOOST_CHECK_EQUAL (b._tz_minute, 0);
        }
+
+       /* Check negative times with non-zero timezone offset minutes */
+       {
+               dcp::LocalTime t ("2013-01-05T18:06:59-04:30");
+               BOOST_CHECK_EQUAL (t._year, 2013);
+               BOOST_CHECK_EQUAL (t._month, 1);
+               BOOST_CHECK_EQUAL (t._day, 5);
+               BOOST_CHECK_EQUAL (t._hour, 18);
+               BOOST_CHECK_EQUAL (t._minute, 6);
+               BOOST_CHECK_EQUAL (t._second, 59);
+               BOOST_CHECK_EQUAL (t._tz_hour, -4);
+               BOOST_CHECK_EQUAL (t._tz_minute, -30);
+               BOOST_CHECK_EQUAL (t.as_string(), "2013-01-05T18:06:59-04:30");
+       }
+
+
 }