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 #include "local_time.h"
21 #include "exceptions.h"
22 #include <boost/lexical_cast.hpp>
23 #include <boost/date_time/c_local_time_adjustor.hpp>
27 using boost::lexical_cast;
30 LocalTime::LocalTime ()
32 time_t now = time (0);
33 struct tm* tm = localtime (&now);
35 _year = tm->tm_year + 1900;
36 _month = tm->tm_mon + 1;
37 _day = tm->tm_mday + 1;
42 set_local_time_zone ();
45 LocalTime::LocalTime (boost::posix_time::ptime t)
47 _year = t.date().year ();
48 _month = t.date().month ();
49 _day = t.date().day ();
50 _hour = t.time_of_day().hours ();
51 _minute = t.time_of_day().minutes ();
52 _second = t.time_of_day().seconds ();
54 set_local_time_zone ();
58 LocalTime::set_local_time_zone ()
60 boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
61 boost::posix_time::ptime const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
62 boost::posix_time::time_duration offset = now - utc_now;
64 _tz_hour = offset.hours ();
65 _tz_minute = offset.minutes ();
68 /** @param s A string of the form 2013-01-05T18:06:59+04:00 */
69 LocalTime::LocalTime (string s)
71 /* 2013-01-05T18:06:59+04:00 */
72 /* 0123456789012345678901234 */
74 if (s.length() < 25) {
75 throw TimeFormatError (s);
78 /* Check incidental characters */
79 if (s[4] != '-' || s[7] != '-' || s[10] != 'T' || s[13] != ':' || s[16] != ':' || s[22] != ':') {
80 throw TimeFormatError (s);
83 _year = lexical_cast<int> (s.substr (0, 4));
84 _month = lexical_cast<int> (s.substr (5, 2));
85 _day = lexical_cast<int> (s.substr (8, 2));
86 _hour = lexical_cast<int> (s.substr (11, 2));
87 _minute = lexical_cast<int> (s.substr (14, 2));
88 _second = lexical_cast<int> (s.substr (17, 2));
89 _tz_hour = lexical_cast<int> (s.substr (20, 2));
90 _tz_minute = lexical_cast<int> (s.substr (23, 2));
94 } else if (s[19] != '+') {
95 throw TimeFormatError (s);
100 LocalTime::as_string () const
104 buffer, sizeof (buffer),
106 date().c_str(), time_of_day().c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
112 LocalTime::date () const
115 snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
120 LocalTime::time_of_day () const
123 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);