diff options
| author | Carl Hetherington <cth@carlh.net> | 2019-12-23 01:05:49 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2019-12-23 01:05:49 +0100 |
| commit | 64e72c622be986c7272e938c2d3235c61abef402 (patch) | |
| tree | 6cece3eee55fd26a79b2aa3c7e618f55258bcefc | |
| parent | 2b5af718d2a1f5da548303c8531afe86fa82d2d8 (diff) | |
Add struct tm constructor for LocalTime, use it to tidy up
day_{greater,less}_than_or_equal and add add_months() method.
| -rw-r--r-- | src/decrypted_kdm.cc | 4 | ||||
| -rw-r--r-- | src/local_time.cc | 26 | ||||
| -rw-r--r-- | src/local_time.h | 4 | ||||
| -rw-r--r-- | src/util.cc | 24 | ||||
| -rw-r--r-- | src/util.h | 4 | ||||
| -rw-r--r-- | test/util_test.cc | 132 |
6 files changed, 82 insertions, 112 deletions
diff --git a/src/decrypted_kdm.cc b/src/decrypted_kdm.cc index 9468aabc..1f68c283 100644 --- a/src/decrypted_kdm.cc +++ b/src/decrypted_kdm.cc @@ -313,9 +313,9 @@ DecryptedKDM::encrypt ( DCP_ASSERT (!_keys.empty ()); BOOST_FOREACH (dcp::Certificate i, signer->leaf_to_root()) { - if (day_greater_than_or_equal(i.not_before(), _not_valid_before)) { + if (day_greater_than_or_equal(dcp::LocalTime(i.not_before()), _not_valid_before)) { throw BadKDMDateError (true); - } else if (day_less_than_or_equal(i.not_after(), _not_valid_after)) { + } else if (day_less_than_or_equal(dcp::LocalTime(i.not_after()), _not_valid_after)) { throw BadKDMDateError (false); } } diff --git a/src/local_time.cc b/src/local_time.cc index 5030500e..2aa55183 100644 --- a/src/local_time.cc +++ b/src/local_time.cc @@ -52,7 +52,17 @@ LocalTime::LocalTime () { time_t now = time (0); struct tm* tm = localtime (&now); + set (tm); +} + +LocalTime::LocalTime (struct tm t) +{ + set (&t); +} +void +LocalTime::set (struct tm const * tm) +{ _year = tm->tm_year + 1900; _month = tm->tm_mon + 1; _day = tm->tm_mday; @@ -208,6 +218,22 @@ LocalTime::time_of_day (bool with_second, bool with_millisecond) const return buffer; } +void +LocalTime::add_months (int m) +{ + _month += m; + + while (_month < 0) { + _month += 12; + _year--; + } + + while (_month > 11) { + _month -= 12; + _year++; + } +} + bool LocalTime::operator== (LocalTime const & other) const { diff --git a/src/local_time.h b/src/local_time.h index f5723783..0b13985c 100644 --- a/src/local_time.h +++ b/src/local_time.h @@ -58,6 +58,7 @@ class LocalTime { public: LocalTime (); + explicit LocalTime (struct tm tm); explicit LocalTime (boost::posix_time::ptime); LocalTime (boost::posix_time::ptime, int tz_hour, int tz_minute); explicit LocalTime (std::string); @@ -82,6 +83,8 @@ public: _year = y; } + void add_months (int a); + bool operator== (LocalTime const & other) const; bool operator!= (LocalTime const & other) const; bool operator< (LocalTime const & other) const; @@ -89,6 +92,7 @@ public: private: friend class ::local_time_test; + void set (struct tm const * tm); void set_local_time_zone (); /* Local time */ diff --git a/src/util.cc b/src/util.cc index 533ee466..9374b6e7 100644 --- a/src/util.cc +++ b/src/util.cc @@ -381,34 +381,34 @@ dcp::indent (xmlpp::Element* element, int initial) * equal to the one represented by \ref b, ignoring the time parts. */ bool -dcp::day_less_than_or_equal (struct tm a, LocalTime b) +dcp::day_less_than_or_equal (LocalTime a, LocalTime b) { - if ((a.tm_year + 1900) != b.year()) { - return (a.tm_year + 1900) < b.year(); + if (a.year() != b.year()) { + return a.year() < b.year(); } - if ((a.tm_mon + 1) != b.month()) { - return (a.tm_mon + 1) < b.month(); + if (a.month() != b.month()) { + return a.month() < b.month(); } - return a.tm_mday <= b.day(); + return a.day() <= b.day(); } /** @return true if the day represented by \ref a is greater than or * equal to the one represented by \ref b, ignoring the time parts. */ bool -dcp::day_greater_than_or_equal (struct tm a, LocalTime b) +dcp::day_greater_than_or_equal (LocalTime a, LocalTime b) { - if ((a.tm_year + 1900) != b.year()) { - return (a.tm_year + 1900) > b.year(); + if (a.year() != b.year()) { + return a.year() > b.year(); } - if ((a.tm_mon + 1) != b.month()) { - return (a.tm_mon + 1) > b.month(); + if (a.month() != b.month()) { + return a.month() > b.month(); } - return a.tm_mday >= b.day(); + return a.day() >= b.day(); } /** Try quite hard to find a string which starts with \ref base and is @@ -76,8 +76,8 @@ extern xmlpp::Node* find_child (xmlpp::Node const * node, std::string name); extern std::string openjpeg_version(); extern std::string spaces (int n); extern void indent (xmlpp::Element* element, int initial); -extern bool day_less_than_or_equal (struct tm a, LocalTime b); -extern bool day_greater_than_or_equal (struct tm a, LocalTime b); +extern bool day_less_than_or_equal (LocalTime a, LocalTime b); +extern bool day_greater_than_or_equal (LocalTime a, LocalTime b); extern std::string unique_string (std::list<std::string> existing, std::string base); } diff --git a/test/util_test.cc b/test/util_test.cc index 24aa5e72..a537909d 100644 --- a/test/util_test.cc +++ b/test/util_test.cc @@ -143,99 +143,56 @@ BOOST_AUTO_TEST_CASE (day_less_than_or_equal_test) { { /* equal */ - struct tm a; - a.tm_mday = 5; - a.tm_mon = 3; - a.tm_year = 78; - + dcp::LocalTime a ("1978-04-05T00:00:00"); dcp::LocalTime b ("1978-04-05T00:00:00"); BOOST_CHECK (day_less_than_or_equal(a, b)); } { /* every part of a less than b */ - struct tm a; - a.tm_mday = 4; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-02-04T00:00:00"); dcp::LocalTime b ("1985-05-23T00:00:00"); BOOST_CHECK (day_less_than_or_equal(a, b)); } { /* years equal, other parts less */ - struct tm a; - a.tm_mday = 4; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-02T00:00:00"); dcp::LocalTime b ("1981-05-10T00:00:00"); BOOST_CHECK (day_less_than_or_equal(a, b)); } { /* year and month equal, day less */ - struct tm a; - a.tm_mday = 4; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-09T00:00:00"); dcp::LocalTime b ("1981-03-12T00:00:00"); BOOST_CHECK (day_less_than_or_equal(a, b)); } { - /* year and month equal, day less */ - struct tm a; - a.tm_mday = 1; - a.tm_mon = 2; - a.tm_year = 81; - - dcp::LocalTime b ("1981-03-04T00:00:00"); - BOOST_CHECK (day_less_than_or_equal(a, b)); - } - - { /* a one day later than b */ - struct tm a; - a.tm_mday = 5; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-05T00:00:00"); dcp::LocalTime b ("1981-03-04T00:00:00"); BOOST_CHECK (!day_less_than_or_equal(a, b)); } { /* year and month same, day much later */ - struct tm a; - a.tm_mday = 22; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-22T00:00:00"); dcp::LocalTime b ("1981-03-04T00:00:00"); BOOST_CHECK (!day_less_than_or_equal(a, b)); } { /* year same, month and day later */ - struct tm a; - a.tm_mday = 22; - a.tm_mon = 5; - a.tm_year = 81; - + dcp::LocalTime a ("1981-06-22T00:00:00"); dcp::LocalTime b ("1981-02-04T00:00:00"); BOOST_CHECK (!day_less_than_or_equal(a, b)); } { /* all later */ - struct tm a; - a.tm_mday = 22; - a.tm_mon = 5; - a.tm_year = 99; - + dcp::LocalTime a ("1999-06-22T00:00:00"); dcp::LocalTime b ("1981-02-04T00:00:00"); BOOST_CHECK (!day_less_than_or_equal(a, b)); } @@ -245,99 +202,63 @@ BOOST_AUTO_TEST_CASE (day_greater_than_or_equal_test) { { /* equal */ - struct tm a; - a.tm_mday = 5; - a.tm_mon = 3; - a.tm_year = 78; - + dcp::LocalTime a ("1978-04-05T00:00:00"); dcp::LocalTime b ("1978-04-05T00:00:00"); BOOST_CHECK (day_greater_than_or_equal(a, b)); } { /* every part of a less than b */ - struct tm a; - a.tm_mday = 4; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-04T00:00:00"); dcp::LocalTime b ("1985-05-23T00:00:00"); BOOST_CHECK (!day_greater_than_or_equal(a, b)); } { /* years equal, other parts less */ - struct tm a; - a.tm_mday = 4; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-02-05T00:00:00"); dcp::LocalTime b ("1981-05-10T00:00:00"); BOOST_CHECK (!day_greater_than_or_equal(a, b)); } { /* year and month equal, day less */ - struct tm a; - a.tm_mday = 4; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-04T00:00:00"); dcp::LocalTime b ("1981-03-12T00:00:00"); BOOST_CHECK (!day_greater_than_or_equal(a, b)); } { /* year and month equal, day less */ - struct tm a; - a.tm_mday = 1; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-01T00:00:00"); dcp::LocalTime b ("1981-03-04T00:00:00"); BOOST_CHECK (!day_greater_than_or_equal(a, b)); } { /* a one day later than b */ - struct tm a; - a.tm_mday = 5; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-05T00:00:00"); dcp::LocalTime b ("1981-03-04T00:00:00"); BOOST_CHECK (day_greater_than_or_equal(a, b)); } { /* year and month same, day much later */ - struct tm a; - a.tm_mday = 22; - a.tm_mon = 2; - a.tm_year = 81; - + dcp::LocalTime a ("1981-03-22T00:00:00"); dcp::LocalTime b ("1981-03-04T00:00:00"); BOOST_CHECK (day_greater_than_or_equal(a, b)); } { /* year same, month and day later */ - struct tm a; - a.tm_mday = 22; - a.tm_mon = 5; - a.tm_year = 81; - + dcp::LocalTime a ("1981-05-22T00:00:00"); dcp::LocalTime b ("1981-02-04T00:00:00"); BOOST_CHECK (day_greater_than_or_equal(a, b)); } { /* all later */ - struct tm a; - a.tm_mday = 22; - a.tm_mon = 5; - a.tm_year = 99; - + dcp::LocalTime a ("1999-06-22T00:00:00"); dcp::LocalTime b ("1981-02-04T00:00:00"); BOOST_CHECK (day_greater_than_or_equal(a, b)); } @@ -353,3 +274,22 @@ BOOST_AUTO_TEST_CASE (unique_string_test) existing.push_back (s); } } + +BOOST_AUTO_TEST_CASE (local_time_add_months_test) +{ + dcp::LocalTime t("2013-06-23T18:06:59.123"); + t.add_months(-1); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2013-05-23T18:06:59.123")); + t.add_months(1); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2013-06-23T18:06:59.123")); + t.add_months(1); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2013-07-23T18:06:59.123")); + t.add_months(4); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2013-11-23T18:06:59.123")); + t.add_months(2); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2014-01-23T18:06:59.123")); + t.add_months(-14); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2012-11-23T18:06:59.123")); + t.add_months(14); + BOOST_CHECK_EQUAL (t, dcp::LocalTime("2014-01-23T18:06:59.123")); +} |
