Add new LocalTime constructor.
[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 /** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset */
71 LocalTime::LocalTime (boost::posix_time::ptime t, int tz_hour, int tz_minute)
72 {
73         _year = t.date().year ();
74         _month = t.date().month ();
75         _day = t.date().day ();
76         _hour = t.time_of_day().hours ();
77         _minute = t.time_of_day().minutes ();
78         _second = t.time_of_day().seconds ();
79         _millisecond = t.time_of_day().fractional_seconds () / 1000;
80         DCP_ASSERT (_millisecond < 1000);
81
82         _tz_hour = tz_hour;
83         _tz_minute = tz_minute;
84 }
85
86 /** Set our UTC offset to be according to the local time zone */
87 void
88 LocalTime::set_local_time_zone ()
89 {
90         boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
91         boost::posix_time::ptime const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
92         boost::posix_time::time_duration offset = now - utc_now;
93
94         _tz_hour = offset.hours ();
95         _tz_minute = offset.minutes ();
96 }
97
98 /** @param s A string of the form 2013-01-05T18:06:59[.123]+04:00 */
99 LocalTime::LocalTime (string s)
100 {
101         /* 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
102         /* 0123456789012345678901234 or 01234567890123456789012345678 */
103
104         if (s.length() < 25) {
105                 throw TimeFormatError (s);
106         }
107
108         /* Check incidental characters */
109         bool const common = s[4] == '-' && s[7] == '-' && s[10] == 'T' && s[13] == ':' && s[16] == ':';
110         bool const without_millisecond = common && s[22] == ':';
111         bool const with_millisecond = common && s[19] == '.' && s[26] == ':';
112
113         if (!with_millisecond && !without_millisecond) {
114                 throw TimeFormatError (s);
115         }
116
117         _year = lexical_cast<int> (s.substr (0, 4));
118         _month = lexical_cast<int> (s.substr (5, 2));
119         _day = lexical_cast<int> (s.substr (8, 2));
120         _hour = lexical_cast<int> (s.substr (11, 2));
121         _minute = lexical_cast<int> (s.substr (14, 2));
122         _second = lexical_cast<int> (s.substr (17, 2));
123         if (without_millisecond) {
124                 _millisecond = 0;
125                 _tz_hour = lexical_cast<int> (s.substr (20, 2));
126                 _tz_minute = lexical_cast<int> (s.substr (23, 2));
127         } else {
128                 _millisecond = lexical_cast<int> (s.substr (20, 3));
129                 _tz_hour = lexical_cast<int> (s.substr (24, 2));
130                 _tz_minute = lexical_cast<int> (s.substr (27, 2));
131         }
132
133         int const plus_minus_position = with_millisecond ? 23 : 19;
134
135         if (s[plus_minus_position] == '-') {
136                 _tz_hour = -_tz_hour;
137         } else if (s[plus_minus_position] != '+') {
138                 throw TimeFormatError (s);
139         }
140 }
141
142 /** @return A string of the form 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
143 string
144 LocalTime::as_string (bool with_millisecond) const
145 {
146         char buffer[32];
147         snprintf (
148                 buffer, sizeof (buffer),
149                 "%sT%s%s%02d:%02d",
150                 date().c_str(), time_of_day(with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
151                 );
152         return buffer;
153 }
154
155 /** @return The date in the form YYYY-MM-DD */
156 string
157 LocalTime::date () const
158 {
159         char buffer[32];
160         snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
161         return buffer;
162 }
163
164 /** @return The time in the form HH:MM:SS or HH:MM:SS.mmm */
165 string
166 LocalTime::time_of_day (bool with_millisecond) const
167 {
168         char buffer[32];
169         if (with_millisecond) {
170                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d.%03d", _hour, _minute, _second, _millisecond);
171         } else {
172                 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
173         }
174         return buffer;
175 }
176
177 bool
178 LocalTime::operator== (LocalTime const & other) const
179 {
180         return _year == other._year && _month == other._month && _day == other._day &&
181                 _hour == other._hour && _second == other._second && _millisecond == other._millisecond &&
182                 _tz_hour == other._tz_hour && _tz_minute == other._tz_minute;
183 }
184
185 bool
186 LocalTime::operator!= (LocalTime const & other) const
187 {
188         return !(*this == other);
189 }
190
191 ostream&
192 dcp::operator<< (ostream& s, LocalTime const & t)
193 {
194         s << t.as_string ();
195         return s;
196 }