/*
*date类是date_time库处理日期的核心类,以天为单位表示时间点概念。
* date支持比较操作和输入输出,我们可以把其当作基本类型来使用
*
* #include<boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
*
* date_duration类,表示日期长度,以天为单位的时长,值可以是任意的整数,可正可负。
* date_time库为date_duration定义了一个常用的typedef::days,此外还提供了months、years、weeks等三个时长,分别表示月年星期
date_period类是用来表示日期范围的概念,它是时间轴上一个左闭右开区间,端点是两个date对象
日期迭代器 可以用++ --操作符连续访问日期,这些迭代器包括如下:
day_iterator、week_iterator、month_iterator和year_iterator
它们并不符合标准迭代器的定义,没有difference_type、pointer、reference等内部类型定义,不能使用advance()或则+=来前进或者后退
ptime类
date_time库有两个组件,处理日期的组件gregorian和处理时间的组件posix_time。
date类用于创建日期,ptime类用于定义时间
ptime是date_time库处理时间的核心类,它使用64位(微秒)或96位(纳秒)的整数在内部存储时间数据
必须包含的头文件:
#include <boost/date_time/posix_time/posix_time.hpp>
命名空间:
using namespace boost::posix_time
time_duration类
类似于日期长度类date_duration有days、weeks、months、years常用类,time_duration有几个子类:
hours、minutes、seconds、millisec、microsec、nanosec,它们都支持流输入输出、比较操作、加减乘除运算
time_period类
类似与日期区间date_period。time_period表示时间轴上一个左闭右开的区间,端点是两个ptime对象
时间迭代器
time_iterator 可以使用++ --操作连续访问时间
日期时间的格式化
date_time库默认的日期格式是英文,看起来不符合日常习惯
date_time库提供专门格式化对象date_facet,time_facet等来搭配IO流,定制日期时间的显示。
*/
示例可执行代码如下:
cpp
#include<iostream>
#include<boost/format.hpp>
// 日期组件
#include<boost/date_time/gregorian/gregorian.hpp>
// 时间组件
#include<boost/date_time/posix_time/posix_time.hpp>
using namespace boost::gregorian;
using namespace boost::posix_time;
using namespace std;
#pragma comment(lib, "libboost_date_time-vc145-mt-gd-x32-1_90.lib")
int main()
{
cout << "create date!!" << endl;
date d1(2001, 1, 1); // 年月日构造日期
date d2 = from_string("2003-02-02");
date d3 = from_string("2004/04/04");
date d4 = from_undelimited_string("20050505");
date d5(min_date_time); // 最小日期 1400-01-01
date d6(max_date_time); // 最大日期 9999-12-31
cout << d1.year() << "-year-" << (int)d1.month() << "-month-" << (int)d1.day() << "-day" << endl;
cout << to_simple_string(d2) << endl; // YYYY-MMM(英文)-DD
cout << to_iso_string(d3) << endl; // YYYYMMDD
cout << to_iso_extended_string(d4) << endl; //YYYY-MM-DD
cout << to_iso_extended_string(d5) << endl;
cout << to_iso_extended_string(d6) << endl;
// 获取今天,day_clock是一个返回当前日期的时钟类
date today = day_clock::local_day();
cout << endl << today.year() << endl;
cout << today.month() << endl;
cout << today.day() << endl;
cout << today.week_number() << endl; // 今年的第几周
cout << today.day_of_week() << endl; // 打印星期几
cout << today.day_of_year() << endl; // 打印一年中的第几天
cout << today.end_of_month() << endl; // 打印本月的最后一天是
days du1(10);
days du2(-5);
date_duration dd1(du1 + du2);
cout <<endl << dd1.days() << endl;
weeks w(3);
cout << "3 weeks in day:" << w.days() << endl;
date_duration dd2(w + du2);
cout << "3 weeks - 5day in day" << dd2.days() << endl;
months m(0); // 无days()成员 一个月无法知道是多少天,并且无法加天数
cout << "0 months months nums:" << m.number_of_months() << endl;
years y(1); // 无days() 无number_of_months()成员函数
// 一年有多少个月
m += y;
cout << "1 year months nums:" << m.number_of_months() << endl;
// 综合使用
cout << endl;
date date1 = date(2026, 1, 31);
days di(3);
cout << to_iso_extended_string(date1 + di) << endl; // 加3天
weeks wi(2);
cout << to_iso_extended_string(date1 + wi) << endl; //加2周
months mi(1);
cout << to_iso_extended_string(date1 + mi) << endl; //加1月
years yi(1);
cout << to_iso_extended_string(date1 + yi) << endl; //加1年
cout << "今天往前推10天是几月几日" << endl;
days dui(-10);
cout << day_clock::local_day() + dui << endl;
//date_period 演示
cout << endl << "演示 date_period" << endl;
date dp1 = date(2000, 1, 1);
date dp2 = date(2000, 2, 1);
date_period dp3(dp1, dp2);
cout << "日期范围:" << dp3 << endl;
cout << "这个范围的天数" << dp3.length() << endl;
date_period dp4(dp1, date_duration(30));
cout << "日期范围:" << dp4 << endl;
dp3.shift(days(3));
cout << "日期范围向后移动3天" << dp3 << endl;
dp4.expand(days(1));
cout << "日期范围向两边扩1天" << dp4 << endl;
//检测某个日期是否在这个日期范围之内
cout << "日期是否包含" << dp3.contains(date(2000, 1, 13)) << endl;
//日期迭代器
cout << endl << "日期迭代器的演示" << endl;
date di1(2000, 1, 1); //原日期一直不产生变化
day_iterator it(di1);
//增减步长默认1天
++it;
cout << *it << endl;
day_iterator it2(di1, 10); //增减步长为10天
++it2;
cout << *it2 << endl;
year_iterator it3(di1, 8);//增减的步长为8年
++it3;
cout << *it3 << endl;
cout << endl << "ptime演示 时间构造" << endl;
ptime p1 = time_from_string("2002-2-2 -2:00:00"); //从字符串生成
ptime p2 = from_iso_string("20030202T030000"); //日期与时间用字母T隔开
ptime p3 = second_clock::local_time(); //本地当前时间,秒精度
ptime p4 = microsec_clock::universal_time(); //UTC当前时间,微秒精度
ptime p5(min_date_time); //特殊值
ptime p6(max_date_time); //特殊值
ptime p7(date(2050, 10, 1)); //时间默认位00:00:00
cout << p1 << " " << p2 << " " << p3 << endl;
cout << p4 << " " << p5 << " " << p6 << endl;
cout << p7 << endl << endl;
// time_duration 构造函数参数 小时 分钟 秒 微妙 单位进制 小时 分钟 秒 毫秒 微妙 纳秒
cout << "时间间隔长度类 time_duration" << endl;
//构造时长:1小时10分钟30秒1毫秒
time_duration td(1, 10, 30, 1000);
cout << td << endl;
//构造时长:2小时01分06.0001秒
time_duration td1(1, 60, 60, 1000 * 1000 * 6 + 1000);
cout << td1 << endl;
//使用字符串构造时长
time_duration td2 = duration_from_string("1:10:30:001");
cout << td2 << endl;
hours h1(1); //构造1小时
cout << h1.total_seconds() << "秒" << h1.total_milliseconds() << "毫秒"
<< h1.total_microseconds() << "微秒" << h1.total_nanoseconds() << "纳秒" << endl;
//构造时长 1小时10分钟30秒1毫秒
time_duration td3 = hours(1) + minutes(10) + seconds(30) + millisec(1);
cout << td3 << endl << endl;
cout << "time_period 时间范围演示" << endl;
ptime per1 = time_from_string("2022-1-1 00:00:01"); //使用工厂函数
ptime per2(date(2022, 1, 2)); //2022-1-1 23:59L59.99999
time_period tper(per1, per2);
cout << tper << endl;
time_duration tder = tper.length(); // 相差的时间
cout << tder << endl;
tper.shift(hours(24)); // 将时间区间向后移动24小时 整体移动
cout << tper << endl;
tper.shift(seconds(10)); // 将时间区间向后移动
cout << tper << endl;
tper.expand(hours(1)); //区间同时向两边扩展1小时
cout << tper << endl;
//某个时间是否存在时间范围内
cout << tper.contains(ptime(date(2032, 1, 2))) << endl << endl;
cout << "时间迭代器" << endl;
ptime p_it = time_from_string("2022-1-1 00:00:00");
//以10秒为步长
time_iterator it_t(p_it, seconds(10));
++it_t;
cout << *it_t << endl;
//以一小时为步长
time_iterator it2_t(p_it, hours(1));
++it2_t;
cout << *it2_t << endl << endl;
cout << "日期格式化演示" << endl;
date d_y = day_clock::local_day();
date_facet* dfacet = new date_facet("%Y年%m月%d日");
//ios_base类的imbue方法指定区域语言 locale类决定程序所使用的当前语言信息
// getloc方法返回流关联的当前语言环境
cout.imbue(locale(cout.getloc(), dfacet));
cout << d_y << endl;
ptime tp_t = microsec_clock::local_time();
time_facet* tfacet = new time_facet("%Y年%m月%d日%H点%M分%S%F秒");
cout.imbue(locale(cout.getloc(), tfacet));
cout << tp_t << endl;
return 0;
}