cadb71379fdda0ad8bb0ab3c954df2637a6ea102
[libdcp.git] / src / local_time.cc
1 /*
2     Copyright (C) 2014-2021 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
35 /** @file  src/local_time.cc
36  *  @brief LocalTime class
37  */
38
39
40 #include "local_time.h"
41 #include "exceptions.h"
42 #include "dcp_assert.h"
43 #include <boost/lexical_cast.hpp>
44 #include <boost/date_time/posix_time/posix_time.hpp>
45 #include <boost/date_time/c_local_time_adjustor.hpp>
46 #include <boost/date_time/gregorian/gregorian.hpp>
47 #include <cstdio>
48
49
50 using std::string;
51 using std::ostream;
52 using boost::lexical_cast;
53 using namespace dcp;
54
55
56 LocalTime::LocalTime ()
57 {
58         auto now = time (0);
59         auto tm = localtime (&now);
60         set (tm);
61         set_local_time_zone ();
62 }
63
64
65 LocalTime::LocalTime (struct tm t)
66 {
67         set (&t);
68         set_local_time_zone ();
69 }
70
71
72 void
73 LocalTime::set (struct tm const * tm)
74 {
75         _year = tm->tm_year + 1900;
76         _month = tm->tm_mon + 1;
77         _day = tm->tm_mday;
78         _hour = tm->tm_hour;
79         _minute = tm->tm_min;
80         _second = tm->tm_sec;
81         _millisecond = 0;
82 }
83
84
85 LocalTime::LocalTime (boost::posix_time::ptime t)
86 {
87         set (t);
88         set_local_time_zone ();
89 }
90
91
92 void
93 LocalTime::set (boost::posix_time::ptime t)
94 {
95         _year = t.date().year ();
96         _month = t.date().month ();
97         _day = t.date().day ();
98         _hour = t.time_of_day().hours ();
99         _minute = t.time_of_day().minutes ();
100         _second = t.time_of_day().seconds ();
101         _millisecond = t.time_of_day().fractional_seconds () / 1000;
102         DCP_ASSERT (_millisecond < 1000);
103 }
104
105
106 LocalTime::LocalTime(boost::posix_time::ptime t, UTCOffset offset)
107 {
108         set (t);
109         _offset = offset;
110 }
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         auto const utc_now = boost::posix_time::second_clock::universal_time ();
118         auto const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
119         auto offset = now - utc_now;
120
121         _offset = { static_cast<int>(offset.hours()), static_cast<int>(offset.minutes()) };
122 }
123
124
125 LocalTime::LocalTime (string s)
126 {
127         /* 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 */
128         /* 0123456789012345678 or 01234567890123456789012 or 0123456789012345678901234 or 01234567890123456789012345678 */
129
130         if (s.length() < 19) {
131                 throw TimeFormatError (s);
132         }
133
134         bool with_millisecond = false;
135         bool with_tz = false;
136
137         switch (s.length ()) {
138         case 19:
139                 break;
140         case 23:
141                 with_millisecond = true;
142                 break;
143         case 25:
144                 with_tz = true;
145                 break;
146         case 29:
147                 with_millisecond = with_tz = true;
148                 break;
149         default:
150                 throw TimeFormatError (s);
151         }
152
153         int const tz_pos = with_millisecond ? 23 : 19;
154
155         /* Check incidental characters */
156         if (s[4] != '-' || s[7] != '-' || s[10] != 'T' || s[13] != ':' || s[16] != ':') {
157                 throw TimeFormatError (s);
158         }
159         if (with_millisecond && s[19] != '.') {
160                 throw TimeFormatError (s);
161         }
162         if (with_tz && s[tz_pos] != '+' && s[tz_pos] != '-') {
163                 throw TimeFormatError (s);
164         }
165
166         _year = lexical_cast<int>(s.substr(0, 4));
167         _month = lexical_cast<int>(s.substr(5, 2));
168         _day = lexical_cast<int>(s.substr(8, 2));
169         _hour = lexical_cast<int>(s.substr(11, 2));
170         _minute = lexical_cast<int>(s.substr(14, 2));
171         _second = lexical_cast<int>(s.substr(17, 2));
172         _millisecond = with_millisecond ? lexical_cast<int>(s.substr(20, 3)) : 0;
173
174         _offset.set_hour(with_tz ? lexical_cast<int>(s.substr(tz_pos + 1, 2)) : 0);
175         _offset.set_minute(with_tz ? lexical_cast<int>(s.substr(tz_pos + 4, 2)) : 0);
176
177         if (with_tz && s[tz_pos] == '-') {
178                 _offset.set_hour(-_offset.hour());
179                 _offset.set_minute(-_offset.minute());
180         }
181 }
182
183
184 string
185 LocalTime::as_string(bool with_millisecond, bool with_timezone) const
186 {
187         char buffer[32];
188
189         auto const written = snprintf(
190                 buffer, sizeof (buffer),
191                 "%sT%s",
192                 date().c_str(), time_of_day(true, with_millisecond).c_str()
193                 );
194
195         DCP_ASSERT(written < 32);
196
197         if (with_timezone) {
198                 snprintf(
199                         buffer + written, sizeof(buffer) - written,
200                         "%s%02d:%02d", (_offset.hour() >= 0 ? "+" : "-"), abs(_offset.hour()), abs(_offset.minute())
201                         );
202         }
203         return buffer;
204 }
205
206
207 string
208 LocalTime::date () const
209 {
210         char buffer[32];
211         snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
212         return buffer;
213 }
214
215
216 string
217 LocalTime::time_of_day (bool with_second, bool with_millisecond) const
218 {
219         char buffer[32];
220         DCP_ASSERT(!(with_millisecond && !with_second));
221         if (with_millisecond) {
222                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d.%03d", _hour, _minute, _second, _millisecond);
223         } else if (with_second) {
224                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
225         } else {
226                 snprintf (buffer, sizeof (buffer), "%02d:%02d", _hour, _minute);
227         }
228         return buffer;
229 }
230
231
232 void
233 LocalTime::add_days (int days)
234 {
235         using namespace boost;
236
237         gregorian::date d (_year, _month, _day);
238         if (days > 0) {
239                 d += gregorian::days (days);
240         } else {
241                 d -= gregorian::days (-days);
242         }
243
244         set (posix_time::ptime(d, posix_time::time_duration(_hour, _minute, _second, _millisecond * 1000)));
245 }
246
247
248 void
249 LocalTime::add(boost::posix_time::time_duration duration)
250 {
251         using namespace boost;
252
253         posix_time::ptime t(gregorian::date(_year, _month, _day), posix_time::time_duration(_hour, _minute, _second, _millisecond * 1000));
254         t += duration;
255         set (t);
256 }
257
258
259 void
260 LocalTime::add_months (int m)
261 {
262         using namespace boost;
263
264         gregorian::date d (_year, _month, _day);
265         if (m > 0) {
266                 d += gregorian::months (m);
267         } else {
268                 d -= gregorian::months (-m);
269         }
270
271         set (posix_time::ptime(d, posix_time::time_duration(_hour, _minute, _second, _millisecond * 1000)));
272 }
273
274
275 void
276 LocalTime::add_minutes (int m)
277 {
278         add(boost::posix_time::time_duration(0, m, 0));
279 }
280
281
282 bool
283 LocalTime::operator== (LocalTime const & other) const
284 {
285         auto a = as_utc();
286         auto b = other.as_utc();
287
288         return a.year() == b.year() && a.month() == b.month() && a.day() == b.day() &&
289                 a.hour() == b.hour() && a.minute() == b.minute() && a.second() == b.second() && a.millisecond() == b.millisecond();
290 }
291
292
293 bool
294 LocalTime::operator< (LocalTime const & other) const
295 {
296         auto a = as_utc();
297         auto b = other.as_utc();
298
299         if (a.year() != b.year()) {
300                 return a.year() < b.year();
301         }
302         if (a.month() != b.month()) {
303                 return a.month() < b.month();
304         }
305         if (a.day() != b.day()) {
306                 return a.day() < b.day();
307         }
308         if (a.hour() != b.hour()) {
309                 return a.hour() < b.hour();
310         }
311         if (a.minute() != b.minute()) {
312                 return a.minute() < other.minute();
313         }
314         if (a.second() != b.second()) {
315                 return a.second() < b.second();
316         }
317         return a.millisecond() < b.millisecond();
318 }
319
320
321 bool
322 LocalTime::operator<=(LocalTime const& other) const
323 {
324         return *this < other || *this == other;
325 }
326
327
328
329 bool
330 LocalTime::operator>(LocalTime const & other) const
331 {
332         auto a = as_utc();
333         auto b = other.as_utc();
334
335         if (a.year() != b.year()) {
336                 return a.year() > b.year();
337         }
338         if (a.month() != b.month()) {
339                 return a.month() > b.month();
340         }
341         if (a.day() != b.day()) {
342                 return a.day() > b.day();
343         }
344         if (a.hour() != b.hour()) {
345                 return a.hour() > b.hour();
346         }
347         if (a.minute() != b.minute()) {
348                 return a.minute() > b.minute();
349         }
350         if (a.second() != b.second()) {
351                 return a.second() > b.second();
352         }
353         return a.millisecond() > b.millisecond();
354 }
355
356
357 bool
358 LocalTime::operator>=(LocalTime const& other) const
359 {
360         return *this > other || *this == other;
361 }
362
363
364 bool
365 LocalTime::operator!= (LocalTime const & other) const
366 {
367         return !(*this == other);
368 }
369
370
371 ostream&
372 dcp::operator<< (ostream& s, LocalTime const & t)
373 {
374         s << t.as_string ();
375         return s;
376 }
377
378
379 LocalTime
380 LocalTime::from_asn1_utc_time (string time)
381 {
382         LocalTime t;
383         sscanf(time.c_str(), "%2d%2d%2d%2d%2d%2d", &t._year, &t._month, &t._day, &t._hour, &t._minute, &t._second);
384
385         if (t._year < 70) {
386                 t._year += 100;
387         }
388         t._year += 1900;
389
390         t._millisecond = 0;
391         t._offset = {};
392
393         return t;
394 }
395
396
397 LocalTime
398 LocalTime::from_asn1_generalized_time (string time)
399 {
400         LocalTime t;
401         sscanf(time.c_str(), "%4d%2d%2d%2d%2d%2d", &t._year, &t._month, &t._day, &t._hour, &t._minute, &t._second);
402
403         t._millisecond = 0;
404         t._offset = {};
405
406         return t;
407 }
408
409
410 LocalTime
411 LocalTime::as_utc() const
412 {
413         auto t = *this;
414         t.add(boost::posix_time::time_duration(-_offset.hour(), -_offset.minute(), 0));
415         return t;
416 }
417