diff options
| author | Carl Hetherington <cth@carlh.net> | 2022-11-29 11:55:26 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-12-13 21:57:52 +0100 |
| commit | 0ece82d184b618a8314bc07d4a447f856847104f (patch) | |
| tree | 1403293713441d84ce2a30421a86de72a20aca63 /src | |
| parent | 70740737a3bdc6a3ac03b7562f466b1dcbf58ff9 (diff) | |
Add UTCOffset class.
Diffstat (limited to 'src')
| -rw-r--r-- | src/local_time.cc | 27 | ||||
| -rw-r--r-- | src/local_time.h | 14 | ||||
| -rw-r--r-- | src/utc_offset.cc | 69 | ||||
| -rw-r--r-- | src/utc_offset.h | 84 | ||||
| -rw-r--r-- | src/wscript | 2 |
5 files changed, 173 insertions, 23 deletions
diff --git a/src/local_time.cc b/src/local_time.cc index ad2291f9..8d48e037 100644 --- a/src/local_time.cc +++ b/src/local_time.cc @@ -103,11 +103,10 @@ LocalTime::set (boost::posix_time::ptime t) } -LocalTime::LocalTime (boost::posix_time::ptime t, int tz_hour, int tz_minute) +LocalTime::LocalTime(boost::posix_time::ptime t, UTCOffset offset) { set (t); - _tz_hour = tz_hour; - _tz_minute = tz_minute; + _offset = offset; } @@ -119,8 +118,7 @@ LocalTime::set_local_time_zone () auto const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now); auto offset = now - utc_now; - _tz_hour = offset.hours (); - _tz_minute = offset.minutes (); + _offset = { static_cast<int>(offset.hours()), static_cast<int>(offset.minutes()) }; } @@ -172,12 +170,13 @@ LocalTime::LocalTime (string s) _minute = lexical_cast<int>(s.substr(14, 2)); _second = lexical_cast<int>(s.substr(17, 2)); _millisecond = with_millisecond ? lexical_cast<int>(s.substr(20, 3)) : 0; - _tz_hour = with_tz ? lexical_cast<int>(s.substr(tz_pos + 1, 2)) : 0; - _tz_minute = with_tz ? lexical_cast<int>(s.substr(tz_pos + 4, 2)) : 0; + + _offset.set_hour(with_tz ? lexical_cast<int>(s.substr(tz_pos + 1, 2)) : 0); + _offset.set_minute(with_tz ? lexical_cast<int>(s.substr(tz_pos + 4, 2)) : 0); if (with_tz && s[tz_pos] == '-') { - _tz_hour = -_tz_hour; - _tz_minute = -_tz_minute; + _offset.set_hour(-_offset.hour()); + _offset.set_minute(-_offset.minute()); } } @@ -189,7 +188,7 @@ LocalTime::as_string (bool with_millisecond) const snprintf ( buffer, sizeof (buffer), "%sT%s%s%02d:%02d", - date().c_str(), time_of_day(true, with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), abs(_tz_minute) + date().c_str(), time_of_day(true, with_millisecond).c_str(), (_offset.hour() >= 0 ? "+" : "-"), abs(_offset.hour()), abs(_offset.minute()) ); return buffer; } @@ -268,7 +267,7 @@ LocalTime::operator== (LocalTime const & other) const { return _year == other._year && _month == other._month && _day == other._day && _hour == other._hour && _second == other._second && _millisecond == other._millisecond && - _tz_hour == other._tz_hour && _tz_minute == other._tz_minute; + _offset == other._offset; } @@ -320,7 +319,8 @@ LocalTime::from_asn1_utc_time (string time) } t._year += 1900; - t._tz_hour = t._tz_minute = t._millisecond = 0; + t._millisecond = 0; + t._offset = {}; return t; } @@ -332,7 +332,8 @@ LocalTime::from_asn1_generalized_time (string time) LocalTime t; sscanf(time.c_str(), "%4d%2d%2d%2d%2d%2d", &t._year, &t._month, &t._day, &t._hour, &t._minute, &t._second); - t._tz_hour = t._tz_minute = t._millisecond = 0; + t._millisecond = 0; + t._offset = {}; return t; } diff --git a/src/local_time.h b/src/local_time.h index c0d8a35e..aacd7728 100644 --- a/src/local_time.h +++ b/src/local_time.h @@ -41,6 +41,7 @@ #define LIBDCP_LOCAL_TIME_H +#include "utc_offset.h" #include <boost/date_time/posix_time/posix_time.hpp> #include <string> @@ -73,11 +74,8 @@ public: */ explicit LocalTime (boost::posix_time::ptime); - /** 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 (boost::posix_time::ptime, int tz_hour, int tz_minute); + /** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset */ + LocalTime(boost::posix_time::ptime, UTCOffset offset); /** @param s A string of the form 2013-01-05T18:06:59[.123][+04:00] */ explicit LocalTime (std::string s); @@ -146,11 +144,7 @@ private: int _second = 0; ///< second number of the minute (0-59) int _millisecond = 0; ///< millisecond number of the second (0-999) - int _tz_hour = 0; ///< hours by which this time is offset from UTC; can be negative - /** Minutes by which this time is offset from UTC; if _tz_hour is negative - * this will be either 0 or negative. - */ - int _tz_minute = 0; + UTCOffset _offset; }; diff --git a/src/utc_offset.cc b/src/utc_offset.cc new file mode 100644 index 00000000..d9abcd60 --- /dev/null +++ b/src/utc_offset.cc @@ -0,0 +1,69 @@ +/* + Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net> + + This file is part of libdcp. + + libdcp is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + libdcp is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libdcp. If not, see <http://www.gnu.org/licenses/>. + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. +*/ + + +#include "dcp_assert.h" +#include "utc_offset.h" + + +using namespace dcp; + + +void +UTCOffset::set_hour(int hour) +{ + DCP_ASSERT(hour >= -11 && hour <= 12); + _hour = hour; +} + + +void +UTCOffset::set_minute(int minute) +{ + DCP_ASSERT(minute >= -30 && minute <= 30); + _minute = minute; +} + + +bool +dcp::operator==(UTCOffset const& a, UTCOffset const& b) +{ + return a.hour() == b.hour() && a.minute() == b.minute(); +} + + +bool +dcp::operator!=(UTCOffset const& a, UTCOffset const& b) +{ + return a.hour() != b.hour() || a.minute() != b.minute(); +} diff --git a/src/utc_offset.h b/src/utc_offset.h new file mode 100644 index 00000000..b29e07cf --- /dev/null +++ b/src/utc_offset.h @@ -0,0 +1,84 @@ +/* + Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net> + + This file is part of libdcp. + + libdcp is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + libdcp is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libdcp. If not, see <http://www.gnu.org/licenses/>. + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. +*/ + + +#ifndef LIBDCP_UTC_OFFSET_H +#define LIBDCP_UTC_OFFSET_H + + +namespace dcp { + + +class UTCOffset +{ +public: + UTCOffset() {} + UTCOffset(int hour, int minute) + : _hour(hour) + , _minute(minute) + {} + + int hour() const { + return _hour; + } + + int minute() const { + return _minute; + } + + void set_hour(int hour); + void set_minute(int hour); + +private: + /** offset such that the equivalent time in UTC can be calculated by + * doing local_time - (_hour, _minute). For example: + * local time UTC _hour _minute + * 09:00 10:00 -1 0 + * 09:00 10:30 -1 -30 + * 03:00 02:00 1 0 + * 03:00 01:30 1 30 + */ + int _hour = 0; + int _minute = 0; +}; + + +bool operator==(UTCOffset const& a, UTCOffset const& b); +bool operator!=(UTCOffset const& a, UTCOffset const& b); + + +} + + +#endif + diff --git a/src/wscript b/src/wscript index e74c3241..39e3351d 100644 --- a/src/wscript +++ b/src/wscript @@ -116,6 +116,7 @@ def build(bld): subtitle_string.cc transfer_function.cc types.cc + utc_offset.cc util.cc verify.cc verify_j2k.cc @@ -215,6 +216,7 @@ def build(bld): subtitle_string.h transfer_function.h types.h + utc_offset.h util.h verify.h verify_j2k.h |
