1、timer
不同於系統函數的timer()一般生成一個定時器,boost中的timer是一個計時器,以秒為單位,最小精度為毫秒,使用需要包含頭文件"boost\timer.hpp",下面是它的使用方法:

boost::timer t; //對象定義后就開始計時
cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl; //輸出最大計時范圍,596.523h
cout << "min timespan: " << t.elapsed_min() << "s" << endl; //輸出最小計時范圍,0.001s
cout << "now time elapsed: " << t.elapsed() << "s" << endl; //輸出當前計時
2、date日期類
data_time庫需要編譯它來使用,它主要包含date日期類、日期長度類days、weeks、months、years,日期迭代器等。data_time只支持1400-9999年之間的日期,如果創建的日期對象使用了其它的值則會拋出異常,使用需要包含頭文件"boost\date_time\gregorian\gregorian.hpp"
下面是定義date日期類對象的方法:

boost::gregorian::date d; //創建了一個無效的日期對象
boost::gregorian::date d1(2010, 1, 1); boost::gregorian::date d2(2000, boost::date_time::Jan, 1); boost::gregorian::date d3 = boost::gregorian::from_string("1999-12-31"); boost::gregorian::date d4 = boost::gregorian::from_string("1999/12/31"); boost::gregorian::date d5 = boost::gregorian::from_undelimited_string("19991231");
date類支持流輸入輸出、比較操作,也包含許多有用的成員函數:

boost::gregorian::date d; //創建了一個無效的日期對象 boost::gregorian::date d1(2010, 1, 1); boost::gregorian::date d2(2000, 1, 1); assert(d == boost::gregorian::date(boost::date_time::not_a_date_time)); //date對象可以進行比較,not_a_date_time表示無效時間 assert(d1 > d2); cout << d1 << endl; //date類對象支持流輸入輸出,輸出d1的日期, 2010-Jan-01 boost::gregorian::date d3 = boost::gregorian::day_clock::local_day(); //獲取當前日期date對象(本地時間) cout << boost::gregorian::day_clock::local_day() << endl; //2018-Apr-10 cout << boost::gregorian::day_clock::universal_day() << endl; //獲取當前日期date對象(UTC時間), 2018-Apr-10 assert(d1.year() == 2010); //獲取年 assert(d1.month() == 1); //獲取月 assert(d1.day() == 1); //獲取日 boost::gregorian::date::ymd_type ymd = d1.year_month_day(); //獲取年月日 assert(ymd.year == 2010); assert(ymd.month == 1); assert(ymd.day == 1); cout << d1.day_of_week() << endl; //獲取是星期幾,0為周日 cout << d1.end_of_month() << endl; //獲取當月最后一天的date對象 cout << d1.day_of_year() << endl; //獲取是當年的第幾天 cout << d1.week_number() << endl; //獲取是當年的第幾周,范圍是0-53,如果年初的幾天位於去年的周則為53
將date類對象轉換為string字符串:

boost::gregorian::date d1(2010, 1, 1); cout << boost::gregorian::to_iso_extended_string(d1) << endl; //2010-01-01 cout << boost::gregorian::to_iso_string(d1) << endl; //20100101 cout << boost::gregorian::to_simple_string(d1) << endl; //2010-Jan-01
date與C中tm結構轉換:

boost::gregorian::date d1(2010, 1, 1); boost::gregorian::to_tm(d1); //將date轉換為tm,tm結構的時分秒均置為0,夏令時標志tm_isdst設為-1表示未知
tm datetm; boost::gregorian::date dat = boost::gregorian::date_from_tm(datetm);//將tm轉換為date,只使用tm的年、月、日三個成員
3、日期長度類days、weeks、months、years
日期長度類date_duration是以天為單位的時長,可正可負,支持==、<、+、-、++、*等運算。使用它的構造函數創建一個日期長度,成員函數days()返回時長的天數,成員函數is_negative()用來判斷是否為負。
date_duration有一個常用的typedef:days,另外類似的還有weeks、months、years三個時長類:

boost::gregorian::date d1(2010, 1, 1); boost::gregorian::days dd1(10), dd2(-100), dd3(255); assert(dd1 > dd2); assert(dd1 + dd2 == boost::gregorian::days(-90)); boost::gregorian::weeks w(3); assert(w.days() == 21); boost::gregorian::months m(5); assert(m.number_of_months() == 5); boost::gregorian::years y(10); d1 += y; assert(d1.year() == 2020);
4、 日期運算
date類對象支持流輸入輸出、比較操作、減法運算,也可以與days、weeks、months、years進行加減運算,date與months、years進行加減運算時,當date的天數是28、29、30時,如果加減運算的結果是2月份,那么date中原來的天數信息會丟失,總是月末最后一天:

boost::gregorian::date d1(2000, 1, 30), d2(2008, 8, 8); int s = (d2 - d1).days(); //3113天
d1 += boost::gregorian::months(1); assert(d1.day() != 30); d1 += boost::gregorian::days(10);
5、日期迭代器
日期迭代器有date_iterator、weak_iterator、month_iterator、year_iterator,他們分別以天、周、月、年為單位增減,日期迭代器在構造的時候需要傳入一個date對象以指明起始日期和增減步長:

boost::gregorian::date d(2006, 11, 10); boost::gregorian::day_iterator d_iter(d); //增減步長默認為1
++d_iter; assert(d_iter->day() == 11);
迭代器的應用示例:

//一個人出生於2000年2月10日,輸出它18歲生日的那個月每天的日期、星期以及計算該月一共有幾個星期天
boost::gregorian::date dBir(2000, 2, 10); boost::gregorian::date d2 = dBir + boost::gregorian::years(18) - boost::gregorian::days(9); boost::gregorian::day_iterator d_iter2(d2); int iCount = 0; for (; d_iter2 <= d2.end_of_month(); ++d_iter2) { cout << *d_iter2 << " " << d_iter2->day_of_week() << endl; if (d_iter2->day_of_week() == boost::gregorian::Sunday) iCount++; } cout << iCount << "Sundays" << endl;
6、其它
判斷閏年:
bool bRet = boost::gregorian::gregorian_calendar::is_leap_year(2010);
date_time庫提供了很多date日期生成器,如某個月的最后一個星期天、第一個星期一等。