Merge branch '1.0' of ssh://main.carlh.net/home/carl/git/libdcp into 1.0
[libdcp.git] / src / local_time.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/local_time.cc
21  *  @brief LocalTime class.
22  */
23
24 #include "local_time.h"
25 #include "exceptions.h"
26 #include "dcp_assert.h"
27 #include <boost/lexical_cast.hpp>
28 #include <boost/date_time/c_local_time_adjustor.hpp>
29 #include <cstdio>
30
31 using std::string;
32 using std::ostream;
33 using boost::lexical_cast;
34 using namespace dcp;
35
36 /** Construct a LocalTime from the current time */
37 LocalTime::LocalTime ()
38 {
39         time_t now = time (0);
40         struct tm* tm = localtime (&now);
41
42         _year = tm->tm_year + 1900;
43         _month = tm->tm_mon + 1;
44         _day = tm->tm_mday;
45         _hour = tm->tm_hour;
46         _minute = tm->tm_min;
47         _second = tm->tm_sec;
48         _millisecond = 0;
49
50         set_local_time_zone ();
51 }
52
53 /** Construct a LocalTime from a boost::posix_time::ptime using the local
54  *  time zone.
55  */
56 LocalTime::LocalTime (boost::posix_time::ptime t)
57 {
58         _year = t.date().year ();
59         _month = t.date().month ();
60         _day = t.date().day ();
61         _hour = t.time_of_day().hours ();
62         _minute = t.time_of_day().minutes ();
63         _second = t.time_of_day().seconds ();
64         _millisecond = t.time_of_day().fractional_seconds () / 1000;
65         DCP_ASSERT (_millisecond < 1000);
66
67         set_local_time_zone ();
68 }
69
70 /** Set our UTC offset to be according to the local time zone */
71 void
72 LocalTime::set_local_time_zone ()
73 {
74         boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
75         boost::posix_time::ptime const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
76         boost::posix_time::time_duration offset = now - utc_now;
77
78         _tz_hour = offset.hours ();
79         _tz_minute = offset.minutes ();
80 }
81
82 /** @param s A string of the form 2013-01-05T18:06:59[.123]+04:00 */
83 LocalTime::LocalTime (string s)
84 {
85         /* 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
86         /* 0123456789012345678901234 or 01234567890123456789012345678 */
87         
88         if (s.length() < 25) {
89                 throw TimeFormatError (s);
90         }
91
92         /* Check incidental characters */
93         bool const common = s[4] == '-' && s[7] == '-' && s[10] == 'T' && s[13] == ':' && s[16] == ':';
94         bool const without_millisecond = common && s[22] == ':';
95         bool const with_millisecond = common && s[19] == '.' && s[26] == ':';
96
97         if (!with_millisecond && !without_millisecond) {
98                 throw TimeFormatError (s);
99         }
100
101         _year = lexical_cast<int> (s.substr (0, 4));
102         _month = lexical_cast<int> (s.substr (5, 2));
103         _day = lexical_cast<int> (s.substr (8, 2));
104         _hour = lexical_cast<int> (s.substr (11, 2));
105         _minute = lexical_cast<int> (s.substr (14, 2));
106         _second = lexical_cast<int> (s.substr (17, 2));
107         if (without_millisecond) {
108                 _millisecond = 0;
109                 _tz_hour = lexical_cast<int> (s.substr (20, 2));
110                 _tz_minute = lexical_cast<int> (s.substr (23, 2));
111         } else {
112                 _millisecond = lexical_cast<int> (s.substr (20, 3));
113                 _tz_hour = lexical_cast<int> (s.substr (24, 2));
114                 _tz_minute = lexical_cast<int> (s.substr (27, 2));
115         }
116
117         int const plus_minus_position = with_millisecond ? 23 : 19;
118
119         if (s[plus_minus_position] == '-') {
120                 _tz_hour = -_tz_hour;
121         } else if (s[plus_minus_position] != '+') {
122                 throw TimeFormatError (s);
123         }
124 }
125
126 /** @return A string of the form 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
127 string
128 LocalTime::as_string (bool with_millisecond) const
129 {
130         char buffer[32];
131         snprintf (
132                 buffer, sizeof (buffer),
133                 "%sT%s%s%02d:%02d",
134                 date().c_str(), time_of_day(with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
135                 );
136         return buffer;
137 }
138
139 /** @return The date in the form YYYY-MM-DD */
140 string
141 LocalTime::date () const
142 {
143         char buffer[32];
144         snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
145         return buffer;
146 }
147
148 /** @return The time in the form HH:MM:SS or HH:MM:SS.mmm */
149 string
150 LocalTime::time_of_day (bool with_millisecond) const
151 {
152         char buffer[32];
153         if (with_millisecond) {
154                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d.%03d", _hour, _minute, _second, _millisecond);
155         } else {
156                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
157         }
158         return buffer;
159 }
160
161 bool
162 LocalTime::operator== (LocalTime const & other) const
163 {
164         return _year == other._year && _month == other._month && _day == other._day &&
165                 _hour == other._hour && _second == other._second && _millisecond == other._millisecond &&
166                 _tz_hour == other._tz_hour && _tz_minute == other._tz_minute;
167 }
168
169 bool
170 LocalTime::operator!= (LocalTime const & other) const
171 {
172         return !(*this == other);
173 }
174
175 ostream&
176 dcp::operator<< (ostream& s, LocalTime const & t)
177 {
178         s << t.as_string ();
179         return s;
180 }