1. local_date_time
#include <boost/date_time/local_time/local_time.hpp> #include <iostream> using namespace boost::local_time; using namespace boost::posix_time; using namespace boost::gregorian; int main() { time_zone_ptr tz(new posix_time_zone( "CET+1")); ptime pt{date{2014, 5, 12}, time_duration{12, 0, 0}}; local_date_time dt{pt, tz}; std::cout << dt.utc_time() << std::endl; std::cout << dt << std::endl; std::cout << dt.local_time() << std::endl; std::cout << dt.zone_name() << std::endl; return 0; }
The constructor of boost::local_time::local_date_time expects its first parameter to be an object of type boost::posix_time::ptime and its second parameter to be an object of type boost::local_time::time_zone_ptr. boost::local_time::time_zone_ptr is a type of definition for boost::shared_ptr<boost::local_time::time_zone>. The type definition is based on boost::local_time::time_zone, not boost::local_time::posix_time_zone.
Values used to initialize objects of type boost::posix_time::ptime and boost::local_time::local_date_time always relate to the UTC time zone by default.
When an object of type boost::local_time::local_date_time
is written to the standard output stream or a call to the member function local_time()
is made, the deviation in hours is used to calculate the local time.
2. local_time_period
#include <boost/date_time/local_time/local_time.hpp> #include <iostream> using namespace boost::local_time; using namespace boost::posix_time; using namespace boost::gregorian; int main() { time_zone_ptr tz(new posix_time_zone("CET+0")); ptime pt1(date(2014, 12, 5), time_duration(12, 0, 0)); local_date_time dt1(pt1, tz); ptime pt2(date(2014, 12, 5), time_duration(18, 0, 0)); local_date_time dt2(pt2, tz); local_time_period tp(dt1, dt2); std::cout.setf(std::ios::boolalpha); std::cout << tp.contains(dt1) << std::endl; std::cout << tp.contains(dt2) << std::endl; return 0; }
The constructor of boost::local_time::local_time_period
in example above expects two parameters of type boost::local_time::local_date_time
. As with other types provided for periods, the second parameter, which represents the end time, is not part of the period.