Hand-merge changes from master (110dc70..979739e)
[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 #include "local_time.h"
21 #include "exceptions.h"
22 #include <boost/lexical_cast.hpp>
23 #include <boost/date_time/c_local_time_adjustor.hpp>
24 #include <cstdio>
25
26 using std::string;
27 using boost::lexical_cast;
28 using namespace dcp;
29
30 LocalTime::LocalTime ()
31 {
32         time_t now = time (0);
33         struct tm* tm = localtime (&now);
34
35         _year = tm->tm_year + 1900;
36         _month = tm->tm_mon + 1;
37         _day = tm->tm_mday + 1;
38         _hour = tm->tm_hour;
39         _minute = tm->tm_min;
40         _second = tm->tm_sec;
41
42         set_local_time_zone ();
43 }
44
45 LocalTime::LocalTime (boost::posix_time::ptime t)
46 {
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 ();
53
54         set_local_time_zone ();
55 }
56
57 void
58 LocalTime::set_local_time_zone ()
59 {
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;
63
64         _tz_hour = offset.hours ();
65         _tz_minute = offset.minutes ();
66 }
67
68 /** @param s A string of the form 2013-01-05T18:06:59+04:00 */
69 LocalTime::LocalTime (string s)
70 {
71         /* 2013-01-05T18:06:59+04:00 */
72         /* 0123456789012345678901234 */
73         
74         if (s.length() < 25) {
75                 throw TimeFormatError (s);
76         }
77
78         /* Check incidental characters */
79         if (s[4] != '-' || s[7] != '-' || s[10] != 'T' || s[13] != ':' || s[16] != ':' || s[22] != ':') {
80                 throw TimeFormatError (s);
81         }
82         
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));
91
92         if (s[19] == '-') {
93                 _tz_hour = -_tz_hour;
94         } else if (s[19] != '+') {
95                 throw TimeFormatError (s);
96         }
97 }
98
99 string
100 LocalTime::as_string () const
101 {
102         char buffer[32];
103         snprintf (
104                 buffer, sizeof (buffer),
105                 "%sT%s%s%02d:%02d",
106                 date().c_str(), time_of_day().c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
107                 );
108         return buffer;
109 }
110
111 string
112 LocalTime::date () const
113 {
114         char buffer[32];
115         snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
116         return buffer;
117 }
118
119 string
120 LocalTime::time_of_day () const
121 {
122         char buffer[32];
123         snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
124         return buffer;
125 }