(一)boost库之日期、时间
生活随笔
收集整理的這篇文章主要介紹了
(一)boost库之日期、时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、計時器?
計時器,通常在一個項目中統計一個函數的執行時間是非常實用的。
?
二、獲取當前日期
date d(day_clock::local_day());?
三、將字符串轉換成日期
date fmtdt1 = from_string("2013-04-21"); date fmtdt2 = date_from_iso_string("20140320"); date fmtdt3 = from_us_string("1-25-2003")?
四、日期轉換成字符串
to_iso_string to_simple_string?
五、日期長度,以天為單位
?
date_duration dur = end - beg; cout << dur.days() << endl;?
六、日期區間
接收兩個時間點,用于判斷時間區間的交集、并集、間隔、區間平移等關于區間的操作
date_period p(beg, end);?
七、日期運算
提供days、months、years三個時間類,可與日期類進行混合運算
months m(1); date end = d + m;?
八、日期、日期與字符串轉換、日期加減運算
#include <boost/date_time/gregorian/gregorian.hpp> void PrintDate() { using namespace boost::gregorian; using namespace std; //獲取當前時間 date d(day_clock::local_day()); //日期類支持IO流輸出 cout << d << endl; //將日期轉換成字符串 20140320 cout << to_iso_string(d) << endl; //將字符串中轉換成日期 date fmtdt1 = from_string("2013-04-21"); date fmtdt2 = from_string("2013/04/21"); cout << fmtdt1 << " " << fmtdt2 << endl; //日期的運算, days day(1); months m(1); years y(1); date beg = d + day - y; date end = d + m; cout << (beg < end ? "Yes" : "No") << endl; //特別地, date - date = date_duration , 同時也支持date +/- date_duration //日期長度 date_duration dur = end - beg; cout << dur.days() << endl; //日期區間 date_period p(beg, dur); cout << p << endl; }?
九、時間、時間與字符串轉換、時間加減運算
和日期大同小異,也提供時間長度:time_duration,時間區間:time_period,及時間操作類hours、minutes、seconds、milliseconds、microseconds
#include <boost/date_time/posix_time/posix_time.hpp> void PrintTime() { using namespace boost::posix_time; using namespace std; using namespace boost::gregorian; //獲取本地時間 ptime t1(second_clock::local_time()); //獲取UTC時間 ptime t2(second_clock::universal_time()); cout << t1 << t2 << endl; //時間8:30 time_duration dur(8,30,0); hours h(1); minutes m(1); seconds s(1); milliseconds mi(1); //毫秒 microseconds mic(1); //微秒 //nanoseconds na(1); //納秒 需要系統的支持 ptime t3(date(2013,4,20)); ptime t4(date(2013,4,20), dur); ptime t5(date(2013,4,20), mic); cout << to_simple_string(t3) <<" " << to_simple_string(t5) << endl; //時間的運算,與日期差不多,非常簡單 cout << t2 - t1 << endl; }?
十、格式化時間
主要用來兩個類,輸入:date_input_facet, 輸出:date_facet?
時間格式化類為:time_input_facet?? time_facet? boost::posix_time::time_facet *timeFmt = new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S");
總結
以上是生活随笔為你收集整理的(一)boost库之日期、时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Boost】boost库asio详解1
- 下一篇: (二)boost库之字符串格式化