date_time用来处理日期和时间,日期是基于格里高日历,支持1400-01-01到9999-12-31之间的日期计算。
#include <iostream>
//处理日期要包含此文件
#include <boost\date_time\gregorian\gregorian.hpp>
//处理时间要包含此文件
#include <boost\date_time\posix_time\posix_time.hpp>
using namespace std;
using namespace boost::gregorian; //对应日期名空间
using namespace boost::posix_time; //对应时间名空间
int main()
{
date d1(from_simple_string("2013-6-22"));
date d2(2013,6,23);
date d3(d2);
//cout<<d1<<endl;
cout<<to_iso_extended_string(d1)<<endl;
//to_simple_string eg.2002-Jan-01
//to_iso_string eg.20021225
//to_iso_extended_string eg.2002-12-31
days d(3); //days ,weeks,months,years表示日期长度,days就是date_duration简写
months m(1);
d1=d1+d+m;
//cout<<d1;
//cout<<day_clock::local_day()<<endl; //输出当前日期2013-Jun-23
date_period dp(date(2010,1,1),days(20)); //日期区间
assert(dp.begin().day()==1);
assert(dp.last().day()==20);
assert(dp.end().day()==21);
assert(dp.length().days()==20);
//cout<<dp; //输出[2010-Jan-01/2010-Jan-20]
time_duration td1(1,10,10,1000); //时间长度, 1小时10分钟10秒1000微秒,默认精确到微秒;
time_duration td2=hours(2)+seconds(23);
assert(td1<td2);
// cout<<td1;
//时间点对象ptime
ptime p(date(2010,3,5),hours(9)+minutes(30));
// cout<<second_clock::local_time()<<endl; //以to_simple_string方式显示 2013-Jun-23 17:25:13
//时间区间
ptime p1(date(2013,6,23)); //没有时间则默认是0时
time_period tp(p1,hours(5)); //时间区间,应该小于1天
cout<<tp<<endl;
}