Change the behaviour of LocalTime::operator== to make the same time in different... v1.8.49
authorCarl Hetherington <cth@carlh.net>
Fri, 30 Dec 2022 17:12:33 +0000 (18:12 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 30 Dec 2022 17:12:52 +0000 (18:12 +0100)
Previously different offsets would mean that the times compared as
not-equal.

src/local_time.cc
src/local_time.h
test/local_time_test.cc

index 9d36f76d3002a3e59119f75e7bbee2b1a6d4275e..659d5c82e307e67cbd2ee0d6b6866424e42c9d3f 100644 (file)
@@ -272,9 +272,11 @@ LocalTime::add_minutes (int m)
 bool
 LocalTime::operator== (LocalTime const & other) const
 {
-       return _year == other._year && _month == other._month && _day == other._day &&
-               _hour == other._hour && _minute == other._minute && _second == other._second && _millisecond == other._millisecond &&
-               _offset == other._offset;
+       auto a = as_utc();
+       auto b = other.as_utc();
+
+       return a.year() == b.year() && a.month() == b.month() && a.day() == b.day() &&
+               a.hour() == b.hour() && a.minute() == b.minute() && a.second() == b.second() && a.millisecond() == b.millisecond();
 }
 
 
index fdb6fc90646bc39988f4ee75bc46b6cd5fc89775..e1baedcc26b894c566c0e25b2d0abe27237703b8 100644 (file)
@@ -60,6 +60,9 @@ namespace dcp {
  *  to parse strings of the required format (those that include time zones).
  *
  *  See http://www.w3.org/TR/xmlschema-2/#dateTime
+ *
+ *  Note that operator== for this class will return true for times that have different
+ *  offsets but are the same actual time.
  */
 class LocalTime
 {
index 72390862dc40b6864c5bc1ee5cbc672153efa700..63083947edff9310a0563eb5abe60f10beec33b7 100644 (file)
@@ -250,4 +250,7 @@ BOOST_AUTO_TEST_CASE(local_time_comparison_test)
        BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:01+01:00") > dcp::LocalTime("2014-10-10T10:00:00+01:00"));
 
        BOOST_CHECK(dcp::LocalTime("2014-01-01T10:00:00") != dcp::LocalTime("2014-01-01T10:05:00"));
+       BOOST_CHECK(dcp::LocalTime("2014-01-01T10:00:00") == dcp::LocalTime("2014-01-01T10:00:00"));
+       BOOST_CHECK(dcp::LocalTime("2014-01-01T10:00:00+02:00") == dcp::LocalTime("2014-01-01T08:00:00"));
+       BOOST_CHECK(dcp::LocalTime("2014-01-01T10:00:00+02:00") == dcp::LocalTime("2014-01-01T11:00:00+03:00"));
 }