[Boost]boost的時間和日期處理-(2)時間的操作
睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接
<開篇>
本篇緊接著boost上篇敘述Boost::DateTime的時間處理。在C++中,常見的時間有time_t, FILETIME和tm,而boost中用ptime。
構造ptime
1.ptime的構造函數有四種:
1: using namespace boost::posix_time;
2: using namespace boost::gregorian;
3: ptime pt(date(2013,Jan,24),time_duration(1,2,3)); //由date和time_duration構造
4: ptime pt1(date(2013,Jan,24),hours()+nanosec(5));//改變形式的time_duration也能使用
5: ptime pt2(p1);//拷貝構造函數
6: ptime pt3(neg_infin);//特殊值構造
7: ptime p;//默認構造函數,這里p等于not_a_date_time
2.用string構造ptime:
1: std::string ts1("2013-01-30 23:32:22.000");//固定格式,小數點后支持6位
2: ptime pt1(time_from_string(ts1));
3: std::string ts2("20130130T233222");//沒有分隔符的date和time
4: ptime pt2(from_iso_string(ts2));
5:
3.通過時鐘構造ptime:
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和FILETIME構造ptime:
1: ptime t = from_time_t(tt); // 其中tt為time_t
2: ptime t1 = from_ftime<ptime>(ft); //其中ft為FILETIME
ptime訪問日期時間
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轉換為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;
ptime與tm,time_t,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和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:
對于這兩者的區別,一個是時間間隔,一個是時間起止的一個窗口。time_duration用于ptime的時間偏移計算為主。而time_period可以計算一個ptime時間點是否在這個時間區間內(參考contains函數)。time_period在創建之后可以擴展,可以平移,函數分別為expand和shift。請大家自己細究。
下一篇將介紹關于boost.datetime的格式化輸入輸出。
<完結>
RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成