43 #include <boost/lexical_cast.hpp>
44 #include <boost/date_time/posix_time/posix_time.hpp>
45 #include <boost/date_time/c_local_time_adjustor.hpp>
46 #include <boost/date_time/gregorian/gregorian.hpp>
52 using boost::lexical_cast;
59 auto tm = localtime (&now);
73 LocalTime::set (
struct tm
const * tm)
75 _year = tm->tm_year + 1900;
93 LocalTime::set (boost::posix_time::ptime t)
95 _year = t.date().year ();
96 _month = t.date().month ();
97 _day = t.date().day ();
98 _hour = t.time_of_day().hours ();
99 _minute = t.time_of_day().minutes ();
100 _second = t.time_of_day().seconds ();
101 _millisecond = t.time_of_day().fractional_seconds () / 1000;
118 auto const utc_now = boost::posix_time::second_clock::universal_time ();
119 auto const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
120 auto offset = now - utc_now;
132 if (s.length() < 19) {
136 bool with_millisecond =
false;
137 bool with_tz =
false;
139 switch (s.length ()) {
143 with_millisecond =
true;
149 with_millisecond = with_tz =
true;
155 int const tz_pos = with_millisecond ? 23 : 19;
158 if (s[4] !=
'-' || s[7] !=
'-' || s[10] !=
'T' || s[13] !=
':' || s[16] !=
':') {
161 if (with_millisecond && s[19] !=
'.') {
164 if (with_tz && s[tz_pos] !=
'+' && s[tz_pos] !=
'-') {
168 _year = lexical_cast<int>(s.substr(0, 4));
169 _month = lexical_cast<int>(s.substr(5, 2));
170 _day = lexical_cast<int>(s.substr(8, 2));
171 _hour = lexical_cast<int>(s.substr(11, 2));
172 _minute = lexical_cast<int>(s.substr(14, 2));
173 _second = lexical_cast<int>(s.substr(17, 2));
174 _millisecond = with_millisecond ? lexical_cast<int>(s.substr(20, 3)) : 0;
175 _tz_hour = with_tz ? lexical_cast<int>(s.substr(tz_pos + 1, 2)) : 0;
176 _tz_minute = with_tz ? lexical_cast<int>(s.substr(tz_pos + 4, 2)) : 0;
178 if (with_tz && s[tz_pos] ==
'-') {
190 buffer,
sizeof (buffer),
202 snprintf (buffer,
sizeof (buffer),
"%04d-%02d-%02d",
_year,
_month,
_day);
211 DCP_ASSERT(!(with_millisecond && !with_second));
212 if (with_millisecond) {
214 }
else if (with_second) {
217 snprintf (buffer,
sizeof (buffer),
"%02d:%02d",
_hour,
_minute);
224 LocalTime::add_days (
int days)
226 using namespace boost;
230 d += gregorian::days (days);
232 d -= gregorian::days (-days);
240 LocalTime::add_months (
int m)
242 using namespace boost;
246 d += gregorian::months (m);
248 d -= gregorian::months (-m);
256 LocalTime::add_minutes (
int m)
258 using namespace boost;
261 t += posix_time::time_duration(0, m, 0);
267 LocalTime::operator== (
LocalTime const & other)
const
276 LocalTime::operator< (
LocalTime const & other)
const
298 LocalTime::operator!= (
LocalTime const & other)
const
300 return !(*
this == other);
305 dcp::operator<< (ostream& s,
LocalTime const & t)
313 LocalTime::from_asn1_utc_time (
string time)
330 LocalTime::from_asn1_generalized_time (
string time)
A representation of a local time (down to the second), including its offset from GMT (equivalent to x...
std::string as_string(bool with_millisecond=false) const
int _day
day number of the month (1-31)
int _hour
hour number of the day (0-23)
int _tz_hour
hours by which this time is offset from UTC; can be negative
int _millisecond
millisecond number of the second (0-999)
std::string time_of_day(bool with_second, bool with_millisecond) const
void set_local_time_zone()
int _second
second number of the minute (0-59)
int _month
month number of the year (1-12)
int _minute
minute number of the hour (0-59)
Exceptions thrown by libdcp.
Namespace for everything in libdcp.