summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2019-12-23 01:05:49 +0100
committerCarl Hetherington <cth@carlh.net>2019-12-23 01:05:49 +0100
commit64e72c622be986c7272e938c2d3235c61abef402 (patch)
tree6cece3eee55fd26a79b2aa3c7e618f55258bcefc /src
parent2b5af718d2a1f5da548303c8531afe86fa82d2d8 (diff)
Add struct tm constructor for LocalTime, use it to tidy up
day_{greater,less}_than_or_equal and add add_months() method.
Diffstat (limited to 'src')
-rw-r--r--src/decrypted_kdm.cc4
-rw-r--r--src/local_time.cc26
-rw-r--r--src/local_time.h4
-rw-r--r--src/util.cc24
-rw-r--r--src/util.h4
5 files changed, 46 insertions, 16 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
diff --git a/src/util.h b/src/util.h
index 2626162a..85b58286 100644
--- a/src/util.h
+++ b/src/util.h
@@ -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);
}