boost准模板库date类使用(直接引入方式)

#define BOOST_DATE_TIME_SOURCE
/*#define BOOST_DATE_TIME_SOIURCE #define BOOST_DATE_TIME_NO_LIB或者#BOOST_ALL_NO_LIB
这些宏定义指明项目对于boost库是源代码级别的引入,将boost库源代码直接嵌入到工程中,不加的话默认找编译后的boost库文件
*/
#include<iostream>
#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<boost/date_time/gregorian/gregorian.hpp>
using namespace std;
using namespace boost::gregorian;
int main()
{
	date d1;
	date d2(2014, 4, 10);
	date d3(2010, Jun, 4);
	date d4(d2);
	date d5 = from_string("1993-03-17");
	date d6 = from_string("2014/4/10");//从字符串转换为日期,年月日之间用/ -分隔
	date d7 = from_undelimited_string("20140410");//从字符串转换为日期,年月日之间没有分隔;

	cout << day_clock::local_day() << endl;//获得当前系统日期
	cout << day_clock::universal_day() << endl;//获得UTC日期
	cout << d1 << endl;
	cout << d2 << endl;
	cout << d3 << endl;
	cout << d4 << endl;
	cout << d5 << endl;
	cout << d6 << endl;
	cout << d7 << endl;//输出日期
	//建立无效日期
	date d8(neg_infin);
	date d9(pos_infin);
	date d10(not_a_date_time);
	date d11(max_date_time);
	date d12(min_date_time);
	cout << d8 << endl;
	cout << d9 << endl;
	cout << d10 << endl;
	cout << d11 << endl;
	cout << d12 << endl;
	//抛出异常的日期
	try{
		date d13(1399, 12, 31);
		date d14(10000, 1, 1);
		date d15(2010, 2, 29);
	}
	catch (exception e)
	{
		cout << e.what() << endl;
	}
	//访问日期的天 月 年
	date d16(day_clock::local_day());
	cout << d16.day() << endl;//获得date的日期
	cout << d16.month() << endl;//获得date的月份
	cout << d16.year() << endl;//获得date的年份
	cout << d16.day_of_week() << endl;//获得date日期在所在周的位置(第一天 第二天)
	cout << d16.day_of_year() << endl;//获得date日期在所在年份的位置
	cout << d16.end_of_month() << endl;//date日期所在月最后一天的日期
	cout << d16.week_number() << endl;//date所在周是当年的第几周,如果年初的几天是去年的周,则返回53,即第0周
	date::ymd_type dd = d16.year_month_day();//获得date::ymd_type即包含年月日的结构
	cout << dd.year << "/" << dd.month << "/" << dd.day << endl;
	//特殊日期判断
	cout << d16.is_infinity() << endl;//无穷
	cout << d16.is_neg_infinity() << endl;//负无穷
	cout << d16.is_pos_infinity() << endl;//正无穷
	cout << d16.is_not_a_date() << endl;//是否是日期
	cout << d16.is_special() << endl;//是否特殊
	//日期转换为字符串
	cout << to_simple_string(d16) << endl;//转换为YYYY-mmm-DD格式
	cout << to_iso_string(d16) << endl;//转换为YYYYMMDD格式
	cout << to_iso_extended_string(d16) << endl;//转换为YYYY-MM-DD
	cout << d16 << endl;//默认与to_iso_extended相同格式
	//cin>>d16;
	//cout<<d16<<endl;
	//date与c里面的tm结构转换
	date d17(2010, 2, 1);
	tm t = to_tm(d17);//date转tm结构,其中时分秒为0 夏令时标志为-1,因为date没有时分秒 夏令时这些成员
	cout << t.tm_year << endl;
	cout << t.tm_mon << endl;
	cout << t.tm_mday << endl;
	cout << t.tm_hour << endl;
	cout << t.tm_min << endl;
	cout << t.tm_sec << endl;
	//tm结构转换date类转换
	t.tm_hour = 8;
	t.tm_min = 59;
	t.tm_sec = 59;
	date d18 = date_from_tm(t);//从tm结构转换成date类型
	cout << (d17 == d18) << endl;
	getchar();
}


上面程序丝对date的基本使用的事例,下图为运行结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值