Tweaks to LocalTime; build/install fixes.
[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 <cstdio>
24
25 using std::string;
26 using boost::lexical_cast;
27 using namespace dcp;
28
29 LocalTime::LocalTime ()
30 {
31         time_t now = time (0);
32         struct tm* tm = localtime (&now);
33
34         _year = tm->tm_year + 1900;
35         _month = tm->tm_mon + 1;
36         _day = tm->tm_mday + 1;
37         _hour = tm->tm_hour;
38         _minute = tm->tm_min;
39         _second = tm->tm_sec;
40
41         set_local_time_zone ();
42 }
43
44 LocalTime::LocalTime (boost::posix_time::ptime t)
45 {
46         _year = t.date().year ();
47         _month = t.date().month ();
48         _day = t.date().day ();
49         _hour = t.time_of_day().hours ();
50         _minute = t.time_of_day().minutes ();
51         _second = t.time_of_day().seconds ();
52
53         set_local_time_zone ();
54 }
55
56 void
57 LocalTime::set_local_time_zone ()
58 {
59         time_t now = time (0);
60         struct tm* tm = localtime (&now);
61
62         int offset = 0;
63         
64 #ifdef LIBDCP_POSIX
65         offset = tm->tm_gmtoff / 60;
66 #else
67         TIME_ZONE_INFORMATION tz;
68         GetTimeZoneInformation (&tz);
69         offset = tz.Bias;
70 #endif
71
72         bool const negative = offset < 0;
73         offset = negative ? -offset : offset;
74
75         _tz_hour = offset / 60;
76         _tz_minute = offset % 60;
77
78         if (negative) {
79                 _tz_hour = -_tz_hour;
80         }
81 }
82
83 /** @param s A string of the form 2013-01-05T18:06:59+04:00 */
84 LocalTime::LocalTime (string s)
85 {
86         /* 2013-01-05T18:06:59+04:00 */
87         /* 0123456789012345678901234 */
88         
89         if (s.length() < 25) {
90                 throw TimeFormatError (s);
91         }
92
93         /* Check incidental characters */
94         if (s[4] != '-' || s[7] != '-' || s[10] != 'T' || s[13] != ':' || s[16] != ':' || s[22] != ':') {
95                 throw TimeFormatError (s);
96         }
97         
98         _year = lexical_cast<int> (s.substr (0, 4));
99         _month = lexical_cast<int> (s.substr (5, 2));
100         _day = lexical_cast<int> (s.substr (8, 2));
101         _hour = lexical_cast<int> (s.substr (11, 2));
102         _minute = lexical_cast<int> (s.substr (14, 2));
103         _second = lexical_cast<int> (s.substr (17, 2));
104         _tz_hour = lexical_cast<int> (s.substr (20, 2));
105         _tz_minute = lexical_cast<int> (s.substr (23, 2));
106
107         if (s[19] == '-') {
108                 _tz_hour = -_tz_hour;
109         } else if (s[19] != '+') {
110                 throw TimeFormatError (s);
111         }
112 }
113
114 string
115 LocalTime::as_string () const
116 {
117         char buffer[32];
118         snprintf (
119                 buffer, sizeof (buffer),
120                 "%sT%s%s%02d:%02d",
121                 date().c_str(), time_of_day().c_str(), (_tz_hour >= 0 ? "+" : "-"), abs (_tz_hour), _tz_minute
122                 );
123         return buffer;
124 }
125
126 string
127 LocalTime::date () const
128 {
129         char buffer[32];
130         snprintf (buffer, sizeof (buffer), "%04d-%02d-%02d", _year, _month, _day);
131         return buffer;
132 }
133
134 string
135 LocalTime::time_of_day () const
136 {
137         char buffer[32];
138         snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d", _hour, _minute, _second);
139         return buffer;
140 }