#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost::gregorian; int _tmain(int argc, _TCHAR* argv[]) { date d1; date d2(2012,12,12); date d3(2012, Jan, 1); date d4(d2); assert(d1==date(not_a_date_time)); assert(d2==d4); d1=from_string("2012-12-10"); d2=(from_string("2012/12/10")); d3=from_undelimited_string("20121210"); cout<<d1<<endl<<d2<<endl<<d3<<endl<<d4<<endl; cout<<day_clock::local_day()<<endl; cout<<day_clock::universal_day()<<endl; return 0; }
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost::gregorian; int _tmain(int argc, _TCHAR* argv[]) { date d(2012,3,26); assert(d.year()==2012); assert(d.month()==3); assert(d.day()==26); //一次性的获得年月日数据 date::ymd_type ymd=d.year_month_day(); assert(ymd.year==2012); assert(ymd.month==3); assert(ymd.day==26); cout<<"今天是"<<d.day_of_week()<<endl; //输出date的星期数,0表示星期日 cout<<"今天是今年的第"<<d.day_of_year()<<"天"<<endl; cout<<"本周是今年的第"<<d.week_number()<<"周"<<endl; cout<<"本月的最后一天是:"<<d.end_of_month()<<endl; //几种输出格式的比较 cout<<to_simple_string(d)<<endl; cout<<to_iso_string(d)<<endl; cout<<to_iso_extended_string(d)<<endl; cout<<d<<endl; return 0; }
日期长度:
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost::gregorian; int _tmain(int argc, _TCHAR* argv[]) { days d1(100); days d2(-100); assert(d1+d2==days(0)); assert((d1+d2).days()==0); weeks w(3); //三个星期 assert(w.days()==21); months m(5); //5个月 years y(2); //2年 months m2=y+m; //2年零5个月 assert(m2.number_of_months()==29); assert(y.number_of_years()*2==4); return 0; }
一些日期的运算:
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost::gregorian; //一些日期的运算 int _tmain(int argc, _TCHAR* argv[]) { date d1(2012,3,26); date d2(1991,2,10); cout<<"我已经出生了"<<d1-d2<<"天了"<<endl; d1+=days(10); cout<<to_iso_extended_string(d1)<<endl; d1+=months(2); cout<<to_iso_extended_string(d1)<<endl; d1+=weeks(1); cout<<to_iso_extended_string(d1)<<endl; d1-=years(2); cout<<to_iso_extended_string(d1)<<endl; return 0; }
日期区间:
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost::gregorian; //日期区间 int _tmain(int argc, _TCHAR* argv[]) { date_period dp(date(2012,1,1),days(10)); assert(!dp.is_null()); assert(dp.begin().day()==1); assert(dp.last().day()==10); assert(dp.end().day()==11); assert(dp.length().days()==10); cout<<dp<<endl; return 0; }
日期区间的运算
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost::gregorian; //日期区间的运算 int _tmain(int argc, _TCHAR* argv[]) { date_period dp(date(2012,1,1),days(10)); dp.shift(days(3)); assert(dp.begin().day()==4); assert(dp.length().days()==10); dp.expand(days(3)); assert(dp.begin().day()==1); assert(dp.length().days()==16); assert(dp.is_after(date(2000,12,12))); assert(dp.is_before(date(2013,12,12))); assert(dp.contains(date(2012,1,2))); return 0; }