Boost库学习随记二 date_time、time_duration、date_facet、time_facet、timer库示例等:

//"Boost_data_Timer.h"头文件

#ifndef BOOST_TEST 
#define BOOST_DATE_TIME_SOURCE
#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 <libs/date_time/src/gregorian/gregorian_types.cpp>

//#include "boost/date_time/gregorian/conversion.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
//date_time库的时间功能位于名字空间boost::posix_time,为了使用时间组件,需要
//包含头文件<boost/date_time/posix_time/posix_time.hpp>即
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;


#endif // !BOOST_TEST 


//"Boost_data_Timer.cpp"

#include <iostream>
#include <cassert>
//#include <Windows.h>
//#include <windef.h>
#include "Boost_data_Timer.h"
using namespace boost;
using namespace std;



//使用date_time日期功能需要包含gregorian.hpp,并声明命名空间。
using namespace boost::gregorian; 

//日期处理测试示例
bool test_date_time()
{
	//date赋值的几种方式
	
		date d1;
		date d2(2010, 1, 1);
		date d3(2000, Jan, 1);
		date d4(d2);
	


	//从字符串产生赋值
	{
	date dd1 = from_string("1999-12-31");
	date dd2 = from_string("2005/1/1");
	date dd3 = from_undelimited_string("20000101");
	//枚举类型特殊时间,超出范围值会报异常。
	date ddd1(neg_infin);  //负无限日期
	date ddd2(pos_infin);  //正无限日期
	date ddd3(not_a_date_time);	//无效日期
	date ddd4(max_date_time);//最大可能日期9999-12031 :)万年虫有没有?
	date ddd5(min_date_time);//最小可能日期1400-01-01

	assert(d1 == date(not_a_date_time));
	assert(d2 == d4);
	assert(d3 < d4);

	}
	
	//auto类型转换时的测试
	if (d2.is_infinity()) //是否为一个无限日期
	{
		auto y = d2.year();
		auto m = d2.month();
		auto d = d2.day();
		cout << y << endl;
		cout << m << endl;
		cout << d << endl;
		cout << d2.year() << endl;
	}
	
	date::ymd_type ymd = d2.year_month_day();
	if (ymd.day)
	{
		auto y = ymd.year;
		auto m = ymd.month;
		auto d = ymd.day;
		cout << y << endl;
		cout << m << endl;
		cout << d << endl;
		//测试auto类型转换
		int a = y;
		cout << a << endl;
		//直接输出ymd
		cout << ymd.year << ymd.month << ymd.day << endl;
	}

	{
		//day_of_week星期数
		date dd2(2010, 01, 01), dd3(2011, 12, 12);
		auto a = dd2.day_of_week();
		cout << "dd2.day_of_week: " << dd2.day_of_week() << endl;
		//day_of_year当年的第几天
		cout << "dd2.day_of_year:" << dd2.day_of_year() << endl;
		//end_of_month返回当月最后一天date对象
		auto b = dd2.end_of_month();
		cout << b.year() << b.month() << b.day() << endl;
		cout << "dd2.end_of_month:" << b.day() << endl;

		//week_number 返回date所在的周是当年的第几周范围0-53
		cout << "week_number: " << dd3.week_number() << endl;
	}

	//is_xxxx相关函数测试
	{
		//is_infinity()			是否是一个无限日期
		//is_neg_infinity()     是否是一个负无限日期
		//is_pos_infinity()     是否是一个正无限日期
		//is_not_a_date()       是否是一个无效日期
		//is_special()          是否是任意一个特殊日期
		assert(date(pos_infin).is_infinity());
		assert(date(pos_infin).is_pos_infinity());
		assert(date(neg_infin).is_neg_infinity());
		assert(date(not_a_date_time).is_not_a_date());
		assert(date(not_a_date_time).is_special());
		assert(!date(2010, 10, 1).is_special());
	}

	//date输出相关示例
	{
		date d(2008, 11, 20);
		//YYYY-MMM-DD
		cout << to_simple_string(d) << endl;
		//YYYYMMDD
		cout 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值