boost準模板庫date類使用(續1 date與日期date 周weeks 年份years 天days計算)

#define BOOST_DATE_TIME_SOURCE
/*#define BOOST_DATE_TIME_SOIURCE #define BOOST_DATE_TIME_NO_LIB或者#BOOST_ALL_NO_LIB
這些宏定義指明項目對於boost庫是源代碼級別的引入,將boost庫源代碼直接嵌入到工程中,不加的話默認找編譯後的boost庫文件
*/
#include<iostream>
#include<libs/date_time/src/gregorian/greg_names.hpp>
#include<libs/date_time/src/gregorian/date_generators.cpp>
#include<libs/date_time/src/gregorian/greg_month.cpp>
#include<libs/date_time/src/gregorian/greg_weekday.cpp>
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace std;
using namespace boost::gregorian;
int main()
{   //日期與days month years weeks類之間的計算
date d1(1991, 5, 1);
date d2(day_clock::local_day());//獲得當前日期
cout << d2 - d1 << endl;//日期之間的天數計算
days temp = d2 - d1;
cout << d1 + temp << endl;//日期與day計算
d1 = d1 + days(100);//日期與day計算
cout << d1 << endl;
cout << d1.month() << endl;
cout << d1.day() << endl;
d1 += months(3);//日期與月類計算
cout << d1 << endl;
d1 -= weeks(4);//日期與weeks計算
cout << d1 << endl;
date d3(1991,5,1);
d3 += years(23);//日期與年份計算
cout << d3 << endl;
cout << d3 + days(pos_infin) << endl;//date與特殊日期的計算
cout << d1 + days(not_a_date_time);
cout << d2 - date(neg_infin) << endl;
//使用月份和日期的特殊情況
date d5(2000, 2, 29);
d5 += months(1);//日期變成2000-3-31
cout << d5 << endl;
d5 += months(1);//日期變成2000-4-30
cout << d5 << endl;
date d4(2000, 2, 29);
cout << d4 + years(1) << endl;//日期變成2000-2-28

getchar();
}


上面例子是對date與其他時間類型的基本計算,操作效果如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章