1. Overview
Header file: # include <boost/date_time/posix_time/posix_time.hpp>
General namespace: Using namespace boost: posix_time;
2. Constructor
Like the date constructor, they are all basic constructor, string constructor, and clock constructor.
Ptime can also be constructed using conversion functions.
Void test_ptime_construct () {using namespace boost: Gregorian; using namespace boost: posix_time; // 1. the following are common constructor types: ptime P0; // The most common constructor ptime p1 (date (2012, 11, 30), time_duration (1, 2, 3 )); // 2012-nov-30 01: 02: 03 ptime P2 (P1); // copy and construct ptime PS1 (neg_infin); ptime PS2 (pos_infin); ptime PS3 (not_a_date_time ); ptime PS4 (max_date_time); // 9999-dec-31ptime ps5 (min_date_time); // 1400-Jan-01 // 2. construct ptime pstr1 (time_from_string ("23:59:59. 000 "); ptime pstr2 (from_iso_string (" 20121130t200001 "); // 3. construct ptime pC1 (second_clock: local_time () through clock; ptime PC2 (second_clock: universal_time (); ptime PC3 (microsec_clock: local_time ()); ptime pc4 (microsec_clock: universal_time (); // 4. use the from_time_t and ptime_from_tm conversion functions. // Note: After from_time_t is converted to ptime, it seems that the Greenwich Mean Time time_t T1 = Time (null); ptime Pc5 = from_time_t (T1 ); STD: cout <Pc5 <STD: Endl; TM * t2 = localtime (& T1); ptime PC6 = ptime_from_tm (* t2); STD :: cout <PC6 <STD: Endl ;}
3. Common functions
Returns the decimal part of the date, time, and second.
Void test_ptime_accessor () {using namespace boost: posix_time; using namespace boost: Gregorian; // obtain the date part ptime Pt (second_clock: local_time (); Date d1 = pt. date (); // obtain the time part time_duration TD = pt. time_of_day (); // obtain the fractional part of the second _ int64 L = TD. fractional_seconds (); STD: cout <L <STD: Endl ;}
4. convert to a string
void test_ptime2string(){using namespace boost::posix_time;time_duration td1(1, 2, 3, 4);std::cout << to_simple_string(td1) << std::endl;std::cout << to_iso_string(td1) << std::endl;}