/*
- Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
This file is part of libdcp.
#include "exceptions.h"
#include "dcp_assert.h"
#include <boost/lexical_cast.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>
+#include <boost/date_time/gregorian/gregorian.hpp>
#include <cstdio>
using std::string;
time_t now = time (0);
struct tm* tm = localtime (&now);
set (tm);
+ set_local_time_zone ();
}
LocalTime::LocalTime (struct tm t)
{
set (&t);
+ set_local_time_zone ();
}
void
_minute = tm->tm_min;
_second = tm->tm_sec;
_millisecond = 0;
-
- set_local_time_zone ();
}
/** Construct a LocalTime from a boost::posix_time::ptime using the local
* time zone.
*/
LocalTime::LocalTime (boost::posix_time::ptime t)
+{
+ set (t);
+ set_local_time_zone ();
+}
+
+void
+LocalTime::set (boost::posix_time::ptime t)
{
_year = t.date().year ();
_month = t.date().month ();
_second = t.time_of_day().seconds ();
_millisecond = t.time_of_day().fractional_seconds () / 1000;
DCP_ASSERT (_millisecond < 1000);
-
- set_local_time_zone ();
}
/** Construct a LocalTime from a boost::posix_time::ptime and a time zone offset.
*/
LocalTime::LocalTime (boost::posix_time::ptime t, int tz_hour, int tz_minute)
{
- _year = t.date().year ();
- _month = t.date().month ();
- _day = t.date().day ();
- _hour = t.time_of_day().hours ();
- _minute = t.time_of_day().minutes ();
- _second = t.time_of_day().seconds ();
- _millisecond = t.time_of_day().fractional_seconds () / 1000;
- DCP_ASSERT (_millisecond < 1000);
-
+ set (t);
_tz_hour = tz_hour;
_tz_minute = tz_minute;
}
return buffer;
}
+
void
-LocalTime::add_months (int m)
+LocalTime::add_days (int days)
{
- _month += m;
+ using namespace boost;
- while (_month < 0) {
- _month += 12;
- _year--;
+ gregorian::date d (_year, _month, _day);
+ if (days > 0) {
+ d += gregorian::days (days);
+ } else {
+ d -= gregorian::days (-days);
}
- while (_month > 11) {
- _month -= 12;
- _year++;
+ set (posix_time::ptime(d, posix_time::time_duration(_hour, _minute, _second, _millisecond * 1000)));
+}
+
+
+void
+LocalTime::add_months (int m)
+{
+ using namespace boost;
+
+ gregorian::date d (_year, _month, _day);
+ if (m > 0) {
+ d += gregorian::months (m);
+ } else {
+ d -= gregorian::months (-m);
}
+
+ set (posix_time::ptime(d, posix_time::time_duration(_hour, _minute, _second, _millisecond * 1000)));
+}
+
+void
+LocalTime::add_minutes (int m)
+{
+ using namespace boost;
+
+ posix_time::ptime t(gregorian::date(_year, _month, _day), posix_time::time_duration(_hour, _minute, _second, _millisecond * 1000));
+ t += posix_time::time_duration(0, m, 0);
+ set (t);
}
bool