diff options
| author | Carl Hetherington <cth@carlh.net> | 2022-12-30 18:11:50 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-12-30 18:12:39 +0100 |
| commit | ef35f6a6c43d2be30b82ca943c56cf06f140d24d (patch) | |
| tree | e32aafcc3128bb5d63de6153e8f8f0e2f7db5c28 /src | |
| parent | 80975a042167710ae24dca887e1a2ba96fe2ad5e (diff) | |
Fix LocalTime::operator< and operator> to handle offset gracefully.
Diffstat (limited to 'src')
| -rw-r--r-- | src/local_time.cc | 66 | ||||
| -rw-r--r-- | src/local_time.h | 1 |
2 files changed, 39 insertions, 28 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 |
