boost的date time库

#include <iostream>
using namespace std;

#include <boost/date_time.hpp>

using namespace boost::gregorian;
using namespace boost::posix_time;

/*
标题:boost的date time库
环境:Windows8.1英文版,Visual Studio 2013 Professional SP1
     boost 1.55
作者:kagula
时间:2014-05
描述:通过代码段演示boost.date time库的使用
备注:
时间段(Time Interval):时间段是两个时间点之间确定的一个区间。
时长(Time Duration) :时长是一个有正负号的标量,是两个时间点之差,不属于数轴。

Gregorian system 只支持从 1400 - Jan - 01 到 9999 - Dec - 31 的时间

补充阅读资料:
PTIME补充阅读资料
http://www.cnblogs.com/yegoufromchongqing/archive/2013/01/24/2875372.html
*/

void date_demo()
{
	//初始化日期类型
	boost::gregorian::date d1(2000, 2, 2);
	boost::gregorian::date d2(2010, Jan, 2);	
	boost::gregorian::date d3(from_string("2000-9-30"));
	boost::gregorian::date d4(from_string("2006/10/30"));
	boost::gregorian::date d5(from_string("1989-June-04"));
	boost::gregorian::date today = day_clock::local_day();//当前日期

	//输出
	cout << d1 << endl;
	cout << d2 << endl;
	cout << d3 << endl;
	cout << d4 << endl;
	cout << d5 << endl;

	cout << today << endl;
	cout << today.year() << endl;//年
	cout << today.month() << endl;//月
	cout << today.day() << endl;//日
	cout << today.day_of_week() << endl;//星期几

	//运算
	cout << d1 + weeks(1) << endl;///<1个星期以后
	cout << d1 + days(3) << endl;///<3天以后
	cout << d1 + months(2) << endl;///<2个月以后

	//判断
	assert(d1 < d2);

	date_period  dp(d1, d2);//日期范围
	assert( dp.contains(date(2006, 10, 01)) );
	assert(dp.contains(date(2000, 02, 02)) );//含上边界
	assert( dp.contains(date(2010, 01, 02))==false );//不包含下边界

	//枚举
	cout << "enumeration demo" << endl;
	month_iterator iter(d1);//输入:2000-Feb-02
	cout << *iter++ << endl;//输出:2000-Mar-02
	cout << *iter++ << endl;//输出:2000-Apr-02
	//超过12,月份又从Mar开始
}

void date_time_demo()
{
	//初始化,ptime在boost::posix_time里
	ptime pt1(date(2013, Jan, 24), time_duration(1, 2, 3)); //由date和time_duration构造
	ptime pt2(pt1);//拷贝构造函数
	ptime pt3(neg_infin);//特殊值构造
	ptime pt4(time_from_string("2010-09-10 10:01:02"));
	ptime now = boost::posix_time::second_clock::local_time();//取当前时间

	//输出
	cout << pt1 << endl;
	assert(std::string("2013-Jan-24 01:02:03")   == to_simple_string(pt1));
	assert(std::string("20130124T010203") == to_iso_string(pt1));
	assert(std::string("2013-01-24T01:02:03") == to_iso_extended_string(pt1));
	assert(std::wstring(L"2013-Jan-24 01:02:03") == to_simple_wstring(pt1));
	assert(std::wstring(L"2010-09-10 10:01:02")  != to_simple_wstring(pt4));
	assert(std::wstring(L"2010-Sep-10 10:01:02") == to_simple_wstring(pt4));

	cout << pt4 << endl;//输出"2010-Sep-10 10:01:02"
	cout << pt4.date().day_of_week() << endl;//输出"Fri"
	cout << pt4.time_of_day() << endl;//输出"10:01:02"

	//运算
	ptime today = now - now.time_of_day();//today的时分秒被置为00。
	boost::posix_time::time_duration timespan = now - today;//时间差
	ptime next_time = now + years(1) + months(1) + days(1) - hours(1);	

	//判断
	assert(next_time > now);
}

int main(int argc, char *argv[])
{
	//date对象测试,时间精度为一天
	date_demo();

	cout << "----------------------------" << endl;

	//ptime对象测试,时间精度为秒
	date_time_demo();

	return 0;
}

你可能感兴趣的:(boost的date time库)