Boost库中date_time库介绍

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;
   
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值