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;
117 auto const utc_now = boost::posix_time::second_clock::universal_time ();
118 auto const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
119 auto offset = now - utc_now;
121 _offset = {
static_cast<int>(offset.hours()),
static_cast<int>(offset.minutes()) };
132 if (s.length() < 19) {
138 if (s[4] !=
'-' || s[7] !=
'-' || s[10] !=
'T' || s[13] !=
':' || s[16] !=
':') {
142 _year = lexical_cast<int>(s.substr(0, 4));
143 _month = lexical_cast<int>(s.substr(5, 2));
144 _day = lexical_cast<int>(s.substr(8, 2));
145 _hour = lexical_cast<int>(s.substr(11, 2));
146 _minute = lexical_cast<int>(s.substr(14, 2));
147 _second = lexical_cast<int>(s.substr(17, 2));
152 if (s.length() > pos && s[pos] ==
'.') {
153 auto end = s.find(
'+', pos);
154 if (end == std::string::npos) {
155 end = s.find(
'-', pos);
157 if (end == std::string::npos) {
160 auto const length = end - pos;
161 _millisecond = lexical_cast<int>(s.substr(pos + 1, std::min(
static_cast<size_t>(3), length - 1)));
168 if (pos != s.length() && s[pos] !=
'Z') {
169 if (s[pos] !=
'+' && s[pos] !=
'-') {
172 if ((s.length() - pos) != 6) {
176 _offset.set_hour(lexical_cast<int>(s.substr(pos + 1, 2)));
177 _offset.set_minute(lexical_cast<int>(s.substr(pos + 4, 2)));
180 _offset.set_hour(-_offset.hour());
181 _offset.set_minute(-_offset.minute());
192 auto const written = snprintf(
193 buffer,
sizeof (buffer),
198 DCP_ASSERT(written < 32);
202 buffer + written,
sizeof(buffer) - written,
203 "%s%02d:%02d", (_offset.hour() >= 0 ?
"+" :
"-"), abs(_offset.hour()), abs(_offset.minute())
214 snprintf (buffer,
sizeof (buffer),
"%04d-%02d-%02d",
_year,
_month,
_day);
223 DCP_ASSERT(!(with_millisecond && !with_second));
224 if (with_millisecond) {
226 }
else if (with_second) {
229 snprintf (buffer,
sizeof (buffer),
"%02d:%02d",
_hour,
_minute);
236 LocalTime::add_days (
int days)
238 using namespace boost;
242 d += gregorian::days (days);
244 d -= gregorian::days (-days);
252 LocalTime::add(boost::posix_time::time_duration duration)
254 using namespace boost;
263 LocalTime::add_months (
int m)
265 using namespace boost;
269 d += gregorian::months (m);
271 d -= gregorian::months (-m);
279 LocalTime::add_minutes (
int m)
281 add(boost::posix_time::time_duration(0, m, 0));
286 LocalTime::operator== (
LocalTime const & other)
const
289 auto b = other.as_utc();
291 return a.year() == b.year() && a.month() == b.month() && a.day() == b.day() &&
292 a.hour() == b.hour() && a.minute() == b.minute() && a.second() == b.second() && a.millisecond() == b.millisecond();
297 LocalTime::operator< (
LocalTime const & other)
const
300 auto b = other.as_utc();
302 if (a.year() != b.year()) {
303 return a.year() < b.year();
305 if (a.month() != b.month()) {
306 return a.month() < b.month();
308 if (a.day() != b.day()) {
309 return a.day() < b.day();
311 if (a.hour() != b.hour()) {
312 return a.hour() < b.hour();
314 if (a.minute() != b.minute()) {
315 return a.minute() < other.minute();
317 if (a.second() != b.second()) {
318 return a.second() < b.second();
320 return a.millisecond() < b.millisecond();
325 LocalTime::operator<=(
LocalTime const& other)
const
327 return *
this < other || *
this == other;
333 LocalTime::operator>(
LocalTime const & other)
const
336 auto b = other.as_utc();
338 if (a.year() != b.year()) {
339 return a.year() > b.year();
341 if (a.month() != b.month()) {
342 return a.month() > b.month();
344 if (a.day() != b.day()) {
345 return a.day() > b.day();
347 if (a.hour() != b.hour()) {
348 return a.hour() > b.hour();
350 if (a.minute() != b.minute()) {
351 return a.minute() > b.minute();
353 if (a.second() != b.second()) {
354 return a.second() > b.second();
356 return a.millisecond() > b.millisecond();
361 LocalTime::operator>=(
LocalTime const& other)
const
363 return *
this > other || *
this == other;
368 LocalTime::operator!= (
LocalTime const & other)
const
370 return !(*
this == other);
375 dcp::operator<< (ostream& s,
LocalTime const & t)
383 LocalTime::from_asn1_utc_time (
string time)
401 LocalTime::from_asn1_generalized_time (
string time)
414 LocalTime::as_utc()
const
417 t.add(boost::posix_time::time_duration(-_offset.hour(), -_offset.minute(), 0));
A representation of a local time (down to the second), including its offset from GMT (equivalent to x...
int _day
day number of the month (1-31)
int _hour
hour number of the day (0-23)
int _millisecond
millisecond number of the second (0-999)
std::string time_of_day(bool with_second, bool with_millisecond) const
std::string as_string(bool with_millisecond=false, bool with_timezone=true) 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.