<Start>
Next to boost, I will describe boost: datetime time processing. In C ++, the common time is time_t, filetime, and TM, while ptime is used in boost.
Construct ptime
1. ptime constructor has four types:
1: using namespace boost::posix_time;
2: using namespace boost::gregorian;
3: ptime Pt (date (2013, Jan, 24), time_duration (, 3); // constructed by date and time_duration
4: ptime pt1 (date (2013, Jan, 24), hours () + nanosec (5); // time_duration in the form of change can also be used
5: ptime pt2 (P1); // copy the constructor
6: ptime pt3 (neg_infin); // special value structure
7: ptime P; // default constructor, where p is not_a_date_time
2. Construct ptime with string:
1: STD: String ts1 ("23:32:22. 000"); // fixed format, supporting 6 digits after decimal point
2: ptime pt1(time_from_string(ts1));
3: STD: String ts2 ("20130130t233222"); // Date and Time without Separators
4: ptime pt2(from_iso_string(ts2));
5:
3. Construct ptime by clock:
1: ptime ct1(second_clock::local_time());
2: ptime ct2(second_clock::universal_time());
3: ptime ct3(microsec_clock::local_time());
4: ptime ct4(microsec_clock::universal_time());
5:
4. time_t and filetime construct ptime:
1: ptime T = from_time_t (TT); // Where TT is time_t
2: ptime T1 = from_ftime <ptime> (FT); // Where ft is filetime
Ptime access date and time
1: using namespace boost::posix_time;
2: using namespace boost::gregorian;
3: ptime now(second_clock::local_time());
4: std::cout << "today is: " << now.date() << std::endl;
5: std::cout << "time is: " << now.time_of_day() << std::endl;
6:
Ptime to string
1: std::string now_str(to_simple_string(now));
2: std::string now_iso_str(to_iso_string(now));
3: std::string now_iso_ext_str(to_iso_extended_string(now));
4: std::cout << now_str << std::endl;
5: std::cout << now_iso_str << std::endl;
6: std::cout << now_iso_ext_str << std::endl;
Mutual conversion between ptime and TM, time_t, and filetime
1. TM
1: using namespace boost::posix_time;
2: using namespace boost::gregorian;
3: tm pt_tm;
4: pt_tm.tm_year = 113;
5: pt_tm.tm_mon = 11;
6: pt_tm.tm_mday = 25;
7: pt_tm.tm_hour = 2;
8: pt_tm.tm_min = 23;
9: pt_tm.tm_sec = 40;
10:
11: ptime pt = data_from_tm(pt_tm);
12: std::cout << pt << std::endl;
13:
14: pt = pt + hours(2);
15: tm pt_tm1 = to_tm(pt);
2. time_t
1: using namespace boost::posix_time;
2: using namespace boost::gregorian;
3:
4: time_t now = time(NULL);
5: std::cout << "time_t : " << now << std::endl;
6: ptime now_pt = from_time_t(now);
7: std::cout << "ptime from time_t : " << now_pt.time_of_day() << std::endl;
8: tm* now_tm = gmtime(&now);
9: std::cout << "tm struct: hour : " << now_tm->tm_hour << std::endl;
10:
3. filetime
1: FILETIME ft;
2: ft.dwHighDateTime = 29715317;
3: ft.dwLowDateTime = 3865122988UL
4: ptime pt = from_ftime<ptime>(ft);
5: // pt ===> 2005-Jun-07 15:30:57.03958200
6:
Time_duration and time_period
1: using namespace boost::posix_time;
2: using namespace boost::gregorian;
3:
4: time_duration td(100,200,3,9);
5: std::cout << td << std::endl;
6: date d(2013,Feb,5);
7: ptime pt(d,minutes(10));
8: ptime pt1(d,hours(10));
9: time_period tp(pt,pt1);
10: std::cout << tp << std::endl;
11:
For the difference between the two, one is the time interval, and the other is a window at the beginning and end of time. Time_duration is mainly used to calculate the Time Offset of ptime. Time_period can be used to calculate whether a ptime point is within this time range (refer to the contains function ). Time_period can be expanded after being created and can be translated. The functions are expand and shift. Please take a closer look.
The next article describes how to format the input and output of boost. datetime.
<CONCLUSION>
[Boost] boost time and date Processing-(2) time operations