The class boost::posix_time::ptime defindes a location-independent time. It uses the type boost::gregorian::date, but also stores a time.
1. ptime
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> using namespace boost::posix_time; using namespace boost::gregorian; int main() { ptime pt(date(2014, 5, 12), time_duration(12, 0, 0)); date d = pt.date(); std::cout << d << std::endl; time_duration td = pt.time_of_day(); std::cout << td << std::endl;
ptime pt2 = second_clock::universal_time();
std::cout << pt2.date() << std::endl;
std::cout << pt2.time_of_day() << std::endl;
pt2 = from_iso_string("20140512T120000");
std::cout << pt2.date() << std::endl;
std::cout << pt2.time_of_day() << std::endl;
return 0; }
To initialize an object of type boost::posix_time::ptime, pass a date of type boost::gregorian::date and a duration of type boost::posix_time::time_duration as the first and second parameters to the constructor. The constructor of boost::posix_time::time_duration takes three parameters, which determine the time. To query date and time, use the member functions date() and time_of_day().
The class boost::posix_time::second_clock returns the current time. The member function universal_time() returns the UTC time. local_time() returns the local time. The free-standing function boost::posix_time::from_iso_string()
converts a time stored in a string formatted using the ISO 8601 standard into an object of type.
2. time_duration
#include <boost/date_time/posix_time/posix_time.hpp> #include <iostream> using namespace boost::posix_time; int main() { time_duration td(16, 30, 0); std::cout << td.hours() << std::endl; std::cout << td.minutes() << std::endl; std::cout << td.seconds() << std::endl; std::cout << td.total_seconds() << std::endl;
ptime pt1{date{2014, 5, 12}, time_duration{12, 0, 0}};
ptime pt2{date{2014, 5, 12}, time_duration{18, 30, 0}};
time_duration td2 = pt2 - pt1;
std::cout << td2.hours() << std::endl;
std::cout << td2.minutes() << std::endl;
std::cout << td2.seconds() << std::endl;
return 0; }
hours(), minutes() and seconds return the respective parts of a time duration, while member functions such as total_seconds(), which returns the total number of seconds.
If two times of type boost::posix_time::ptime are subtracted from each other, the result in an object of type boost::posix_time::time_duration that specifies the duration between the two times.
3. time_period
#include <boost/date_time/posix_time/posix_time.hpp> #include <iostream> using namespace boost::posix_time; using namespace boost::gregorian; int main() { ptime pt1(date(2014, 5, 12), time_duration(12, 0, 0)); ptime pt2(date(2014, 5, 12), time_duration(18, 30, 0)); time_period tp{pt1, pt2}; std::cout.setf(std::ios::boolalpha); std::cout << tp.contains(pt1) << std::endl; std::cout << tp.contains(pt2) << std::endl; return 0; }
In general, boost::posix_time::time_period
works just like boost::gregorian::date_period
. It provides a member function, contains()
, which returns true
for every point in time within the period. Because the end time, which is passed to the constructor of boost::posix_time::time_period
, is not part of the period, the second call to contains()
in example above returns false
.
4. iterator
#include <boost/date_time/local_time/local_time.hpp> #include <iostream> using namespace boost::posix_time; using namespace boost::gregorian; int main() { ptime pt(date(2014, 5, 12), time_duration(12, 0, 0)); time_iterator it(pt, time_duration(6, 30, 0)); std::cout << *++it << std::endl; std::cout << *++it << std::endl; return 0; }
Example above uses the iterator it to jump forward 6.5 hours from the time pt. Because the iterator is increamented twice,, the output is 2014-May-12 18:30:00
and 2014-May-13 01:00:00
.