Fix erroneous reports of unresolved assets when checking OV/VF pairs.
[libdcp.git] / src / local_time.cc
index 8a0092096caba28dfb66dd11d15302b95738ccbc..5030500efdc8b7a255b0eb618854e2440bf16d88 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
@@ -81,7 +81,10 @@ LocalTime::LocalTime (boost::posix_time::ptime t)
        set_local_time_zone ();
 }
 
-/** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset */
+/** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset.
+ *  @param tz_minute Offset from UTC in minutes; if the timezone is behind UTC this may be negative,
+ *  e.g. -04:30 would have tz_hour=-1 and tz_minute=-30.
+ */
 LocalTime::LocalTime (boost::posix_time::ptime t, int tz_hour, int tz_minute)
 {
        _year = t.date().year ();
@@ -163,6 +166,7 @@ LocalTime::LocalTime (string s)
 
        if (with_tz && s[tz_pos] == '-') {
                _tz_hour = -_tz_hour;
+               _tz_minute = -_tz_minute;
        }
 }
 
@@ -174,7 +178,7 @@ LocalTime::as_string (bool with_millisecond) const
        snprintf (
                buffer, sizeof (buffer),
                "%sT%s%s%02d:%02d",
-               date().c_str(), time_of_day(with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
+               date().c_str(), time_of_day(true, with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), abs(_tz_minute)
                );
        return buffer;
 }
@@ -190,13 +194,16 @@ LocalTime::date () const
 
 /** @return The time in the form HH:MM:SS or HH:MM:SS.mmm */
 string
-LocalTime::time_of_day (bool with_millisecond) const
+LocalTime::time_of_day (bool with_second, bool with_millisecond) const
 {
        char buffer[32];
+       DCP_ASSERT(!(with_millisecond && !with_second));
        if (with_millisecond) {
                snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d.%03d", _hour, _minute, _second, _millisecond);
-       } else {
+       } else if (with_second) {
                snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
+       } else {
+               snprintf (buffer, sizeof (buffer), "%02d:%02d", _hour, _minute);
        }
        return buffer;
 }
@@ -209,6 +216,27 @@ LocalTime::operator== (LocalTime const & other) const
                _tz_hour == other._tz_hour && _tz_minute == other._tz_minute;
 }
 
+bool
+LocalTime::operator< (LocalTime const & other) const
+{
+       if (_year != other._year) {
+               return _year < other._year;
+       }
+       if (_month != other._month) {
+               return _month < other._month;
+       }
+       if (_day != other._day) {
+               return _day < other._day;
+       }
+       if (_hour != other._hour) {
+               return _hour < other._hour;
+       }
+       if (_second != other._second) {
+               return _second < other._second;
+       }
+       return _millisecond < other._millisecond;
+}
+
 bool
 LocalTime::operator!= (LocalTime const & other) const
 {