boost location-independent times
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 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,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,0)); ptime pt2(date(2014,time_duration(18,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, 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,0)); time_iterator it(pt,time_duration(6,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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |