Merge.
[libdcp.git] / src / local_time.cc
1 /*
2     Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/local_time.cc
35  *  @brief LocalTime class.
36  */
37
38 #include "local_time.h"
39 #include "exceptions.h"
40 #include "dcp_assert.h"
41 #include <boost/lexical_cast.hpp>
42 #include <boost/date_time/c_local_time_adjustor.hpp>
43 #include <cstdio>
44
45 using std::string;
46 using std::ostream;
47 using boost::lexical_cast;
48 using namespace dcp;
49
50 /** Construct a LocalTime from the current time */
51 LocalTime::LocalTime ()
52 {
53         time_t now = time (0);
54         struct tm* tm = localtime (&now);
55         set (tm);
56 }
57
58 LocalTime::LocalTime (struct tm t)
59 {
60         set (&t);
61 }
62
63 void
64 LocalTime::set (struct tm const * tm)
65 {
66         _year = tm->tm_year + 1900;
67         _month = tm->tm_mon + 1;
68         _day = tm->tm_mday;
69         _hour = tm->tm_hour;
70         _minute = tm->tm_min;
71         _second = tm->tm_sec;
72         _millisecond = 0;
73
74         set_local_time_zone ();
75 }
76
77 /** Construct a LocalTime from a boost::posix_time::ptime using the local
78  *  time zone.
79  */
80 LocalTime::LocalTime (boost::posix_time::ptime t)
81 {
82         _year = t.date().year ();
83         _month = t.date().month ();
84         _day = t.date().day ();
85         _hour = t.time_of_day().hours ();
86         _minute = t.time_of_day().minutes ();
87         _second = t.time_of_day().seconds ();
88         _millisecond = t.time_of_day().fractional_seconds () / 1000;
89         DCP_ASSERT (_millisecond < 1000);
90
91         set_local_time_zone ();
92 }
93
94 /** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset.
95  *  @param tz_minute Offset from UTC in minutes; if the timezone is behind UTC this may be negative,
96  *  e.g. -04:30 would have tz_hour=-1 and tz_minute=-30.
97  */
98 LocalTime::LocalTime (boost::posix_time::ptime t, int tz_hour, int tz_minute)
99 {
100         _year = t.date().year ();
101         _month = t.date().month ();
102         _day = t.date().day ();
103         _hour = t.time_of_day().hours ();
104         _minute = t.time_of_day().minutes ();
105         _second = t.time_of_day().seconds ();
106         _millisecond = t.time_of_day().fractional_seconds () / 1000;
107         DCP_ASSERT (_millisecond < 1000);
108
109         _tz_hour = tz_hour;
110         _tz_minute = tz_minute;
111 }
112
113 /** Set our UTC offset to be according to the local time zone */
114 void
115 LocalTime::set_local_time_zone ()
116 {
117         boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
118         boost::posix_time::ptime const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
119         boost::posix_time::time_duration offset = now - utc_now;
120
121         _tz_hour = offset.hours ();
122         _tz_minute = offset.minutes ();
123 }
124
125 /** @param s A string of the form 2013-01-05T18:06:59[.123][+04:00] */
126 LocalTime::LocalTime (string s)
127 {
128         /* 2013-01-05T18:06:59 or 2013-01-05T18:06:59.123 or 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
129         /* 0123456789012345678 or 01234567890123456789012 or 0123456789012345678901234 or 01234567890123456789012345678 */
130
131         if (s.length() < 19) {
132                 throw TimeFormatError (s);
133         }
134
135         bool with_millisecond = false;
136         bool with_tz = false;
137
138         switch (s.length ()) {
139         case 19:
140                 break;
141         case 23:
142                 with_millisecond = true;
143                 break;
144         case 25:
145                 with_tz = true;
146                 break;
147         case 29:
148                 with_millisecond = with_tz = true;
149                 break;
150         default:
151                 throw TimeFormatError (s);
152         }
153
154         int const tz_pos = with_millisecond ? 23 : 19;
155
156         /* Check incidental characters */
157         if (s[4] != '-' || s[7] != '-' || s[10] != 'T' || s[13] != ':' || s[16] != ':') {
158                 throw TimeFormatError (s);
159         }
160         if (with_millisecond && s[19] != '.') {
161                 throw TimeFormatError (s);
162         }
163         if (with_tz && s[tz_pos] != '+' && s[tz_pos] != '-') {
164                 throw TimeFormatError (s);
165         }
166
167         _year = lexical_cast<int> (s.substr (0, 4));
168         _month = lexical_cast<int> (s.substr (5, 2));
169         _day = lexical_cast<int> (s.substr (8, 2));
170         _hour = lexical_cast<int> (s.substr (11, 2));
171         _minute = lexical_cast<int> (s.substr (14, 2));
172         _second = lexical_cast<int> (s.substr (17, 2));
173         _millisecond = with_millisecond ? lexical_cast<int> (s.substr (20, 3)) : 0;
174         _tz_hour = with_tz ? lexical_cast<int> (s.substr (tz_pos + 1, 2)) : 0;
175         _tz_minute = with_tz ? lexical_cast<int> (s.substr (tz_pos + 4, 2)) : 0;
176
177         if (with_tz && s[tz_pos] == '-') {
178                 _tz_hour = -_tz_hour;
179                 _tz_minute = -_tz_minute;
180         }
181 }
182
183 /** @return A string of the form 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
184 string
185 LocalTime::as_string (bool with_millisecond) const
186 {
187         char buffer[32];
188         snprintf (
189                 buffer, sizeof (buffer),
190                 "%sT%s%s%02d:%02d",
191                 date().c_str(), time_of_day(true, with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), abs(_tz_minute)
192                 );
193         return buffer;
194 }
195
196 /** @return The date in the form YYYY-MM-DD */
197 string
198 LocalTime::date () const
199 {
200         char buffer[32];
201         snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
202         return buffer;
203 }
204
205 /** @return The time in the form HH:MM:SS or HH:MM:SS.mmm */
206 string
207 LocalTime::time_of_day (bool with_second, bool with_millisecond) const
208 {
209         char buffer[32];
210         DCP_ASSERT(!(with_millisecond && !with_second));
211         if (with_millisecond) {
212                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d.%03d", _hour, _minute, _second, _millisecond);
213         } else if (with_second) {
214                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
215         } else {
216                 snprintf (buffer, sizeof (buffer), "%02d:%02d", _hour, _minute);
217         }
218         return buffer;
219 }
220
221 void
222 LocalTime::add_months (int m)
223 {
224         _month += m;
225
226         while (_month < 0) {
227                 _month += 12;
228                 _year--;
229         }
230
231         while (_month > 11) {
232                 _month -= 12;
233                 _year++;
234         }
235 }
236
237 bool
238 LocalTime::operator== (LocalTime const & other) const
239 {
240         return _year == other._year && _month == other._month && _day == other._day &&
241                 _hour == other._hour && _second == other._second && _millisecond == other._millisecond &&
242                 _tz_hour == other._tz_hour && _tz_minute == other._tz_minute;
243 }
244
245 bool
246 LocalTime::operator< (LocalTime const & other) const
247 {
248         if (_year != other._year) {
249                 return _year < other._year;
250         }
251         if (_month != other._month) {
252                 return _month < other._month;
253         }
254         if (_day != other._day) {
255                 return _day < other._day;
256         }
257         if (_hour != other._hour) {
258                 return _hour < other._hour;
259         }
260         if (_second != other._second) {
261                 return _second < other._second;
262         }
263         return _millisecond < other._millisecond;
264 }
265
266 bool
267 LocalTime::operator!= (LocalTime const & other) const
268 {
269         return !(*this == other);
270 }
271
272 ostream&
273 dcp::operator<< (ostream& s, LocalTime const & t)
274 {
275         s << t.as_string ();
276         return s;
277 }