2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/local_time.cc
21 * @brief LocalTime class.
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>
33 using boost::lexical_cast;
36 /** Construct a LocalTime from the current time */
37 LocalTime::LocalTime ()
39 time_t now = time (0);
40 struct tm* tm = localtime (&now);
42 _year = tm->tm_year + 1900;
43 _month = tm->tm_mon + 1;
50 set_local_time_zone ();
53 /** Construct a LocalTime from a boost::posix_time::ptime using the local
56 LocalTime::LocalTime (boost::posix_time::ptime t)
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);
67 set_local_time_zone ();
70 /** Set our UTC offset to be according to the local time zone */
72 LocalTime::set_local_time_zone ()
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;
78 _tz_hour = offset.hours ();
79 _tz_minute = offset.minutes ();
82 /** @param s A string of the form 2013-01-05T18:06:59[.123]+04:00 */
83 LocalTime::LocalTime (string s)
85 /* 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
86 /* 0123456789012345678901234 or 01234567890123456789012345678 */
88 if (s.length() < 25) {
89 throw TimeFormatError (s);
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] == ':';
97 if (!with_millisecond && !without_millisecond) {
98 throw TimeFormatError (s);
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) {
109 _tz_hour = lexical_cast<int> (s.substr (20, 2));
110 _tz_minute = lexical_cast<int> (s.substr (23, 2));
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));
117 int const plus_minus_position = with_millisecond ? 23 : 19;
119 if (s[plus_minus_position] == '-') {
120 _tz_hour = -_tz_hour;
121 } else if (s[plus_minus_position] != '+') {
122 throw TimeFormatError (s);
126 /** @return A string of the form 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */
128 LocalTime::as_string (bool with_millisecond) const
132 buffer, sizeof (buffer),
134 date().c_str(), time_of_day(with_millisecond).c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
139 /** @return The date in the form YYYY-MM-DD */
141 LocalTime::date () const
144 snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
148 /** @return The time in the form HH:MM:SS or HH:MM:SS.mmm */
150 LocalTime::time_of_day (bool with_millisecond) const
153 if (with_millisecond) {
154 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d.%03d", _hour, _minute, _second, _millisecond);
156 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
162 LocalTime::operator== (LocalTime const & other) const
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;
170 LocalTime::operator!= (LocalTime const & other) const
172 return !(*this == other);
176 dcp::operator<< (ostream& s, LocalTime const & t)