summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2022-12-30 18:11:50 +0100
committerCarl Hetherington <cth@carlh.net>2022-12-30 18:12:39 +0100
commitef35f6a6c43d2be30b82ca943c56cf06f140d24d (patch)
treee32aafcc3128bb5d63de6153e8f8f0e2f7db5c28
parent80975a042167710ae24dca887e1a2ba96fe2ad5e (diff)
Fix LocalTime::operator< and operator> to handle offset gracefully.
-rw-r--r--src/local_time.cc66
-rw-r--r--src/local_time.h1
-rw-r--r--test/local_time_test.cc21
3 files changed, 59 insertions, 29 deletions
diff --git a/src/local_time.cc b/src/local_time.cc
index f60fb0a6..9d36f76d 100644
--- a/src/local_time.cc
+++ b/src/local_time.cc
@@ -281,54 +281,56 @@ LocalTime::operator== (LocalTime const & other) const
bool
LocalTime::operator< (LocalTime const & other) const
{
- DCP_ASSERT(_offset == other._offset);
+ auto a = as_utc();
+ auto b = other.as_utc();
- if (_year != other._year) {
- return _year < other._year;
+ if (a.year() != b.year()) {
+ return a.year() < b.year();
}
- if (_month != other._month) {
- return _month < other._month;
+ if (a.month() != b.month()) {
+ return a.month() < b.month();
}
- if (_day != other._day) {
- return _day < other._day;
+ if (a.day() != b.day()) {
+ return a.day() < b.day();
}
- if (_hour != other._hour) {
- return _hour < other._hour;
+ if (a.hour() != b.hour()) {
+ return a.hour() < b.hour();
}
- if (_minute != other._minute) {
- return _minute < other._minute;
+ if (a.minute() != b.minute()) {
+ return a.minute() < other.minute();
}
- if (_second != other._second) {
- return _second < other._second;
+ if (a.second() != b.second()) {
+ return a.second() < b.second();
}
- return _millisecond < other._millisecond;
+ return a.millisecond() < b.millisecond();
}
bool
LocalTime::operator>(LocalTime const & other) const
{
- DCP_ASSERT(_offset == other._offset);
+ auto a = as_utc();
+ auto b = other.as_utc();
- if (_year != other._year) {
- return _year > other._year;
+ if (a.year() != b.year()) {
+ return a.year() > b.year();
}
- if (_month != other._month) {
- return _month > other._month;
+ if (a.month() != b.month()) {
+ return a.month() > b.month();
}
- if (_day != other._day) {
- return _day > other._day;
+ if (a.day() != b.day()) {
+ return a.day() > b.day();
}
- if (_hour != other._hour) {
- return _hour > other._hour;
+ if (a.hour() != b.hour()) {
+ return a.hour() > b.hour();
}
- if (_minute != other._minute) {
- return _minute > other._minute;
+ if (a.minute() != b.minute()) {
+ return a.minute() > b.minute();
}
- if (_second != other._second) {
- return _second > other._second;
+ if (a.second() != b.second()) {
+ return a.second() > b.second();
}
- return _millisecond > other._millisecond;
+ return a.millisecond() > b.millisecond();
}
@@ -378,3 +380,11 @@ LocalTime::from_asn1_generalized_time (string time)
}
+LocalTime
+LocalTime::as_utc() const
+{
+ auto t = *this;
+ t.add(boost::posix_time::time_duration(-_offset.hour(), -_offset.minute(), 0));
+ return t;
+}
+
diff --git a/src/local_time.h b/src/local_time.h
index f0fa4b63..fdb6fc90 100644
--- a/src/local_time.h
+++ b/src/local_time.h
@@ -162,6 +162,7 @@ private:
void set (struct tm const * tm);
void set (boost::posix_time::ptime);
void set_local_time_zone ();
+ dcp::LocalTime as_utc() const;
/* Local time */
int _year = 0; ///< year
diff --git a/test/local_time_test.cc b/test/local_time_test.cc
index c38b2d5c..72390862 100644
--- a/test/local_time_test.cc
+++ b/test/local_time_test.cc
@@ -228,7 +228,26 @@ BOOST_AUTO_TEST_CASE (local_time_from_asn1_generalized_time_test)
BOOST_AUTO_TEST_CASE(local_time_comparison_test)
{
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("2015-01-01T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-01-01T10:00:00") < dcp::LocalTime("2014-01-01T11:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00") < dcp::LocalTime("2014-10-10T10:00:01"));
+ BOOST_CHECK(!(dcp::LocalTime("2014-10-10T10:00:00") < dcp::LocalTime("2014-10-10T10:00:00")));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00+01:00") < dcp::LocalTime("2014-10-10T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00+01:30") < dcp::LocalTime("2014-10-10T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00+01:00") < dcp::LocalTime("2014-10-10T10:00:01+01:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-01-01T10:00:00") < dcp::LocalTime("2014-01-01T10:05:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00") < dcp::LocalTime("2014-10-10T10:00:00-01:30"));
+
+ BOOST_CHECK(dcp::LocalTime("2014-01-01T10:05:00") > dcp::LocalTime("2014-01-01T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00-01:30") > dcp::LocalTime("2014-10-10T10:00:00"));
BOOST_CHECK(dcp::LocalTime("2014-01-01T10:05:00") > dcp::LocalTime("2014-01-01T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2015-01-01T10:00:00") > dcp::LocalTime("2014-01-01T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-01-01T11:00:00") > dcp::LocalTime("2014-01-01T10:00:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:01") > dcp::LocalTime("2014-10-10T10:00:00"));
+ BOOST_CHECK(!(dcp::LocalTime("2014-10-10T10:00:00") > dcp::LocalTime("2014-10-10T10:00:00")));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00") > dcp::LocalTime("2014-10-10T10:00:00+01:00"));
+ BOOST_CHECK(dcp::LocalTime("2014-10-10T10:00:00") > dcp::LocalTime("2014-10-10T10:00:00+01:30"));
+ 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"));
}
-