timer库:timer、progress_timer、progress_display三个组件。
1.timer:
1)elapsed_max():表示时间的最大表示范围。
2)elapsed_min():表示时间的最小表示范围。
3)elapsed():表示从timer结构体到elapsed()语句之间的时间开销。
2.progress_timer:和timer不同之处在于不需要显式地使用elapsed()来输出时间。
3.progress_display:用于显示进度条,但是存在进度输出和程序输出结合在一起的混乱情况。
1)<boost/progress.hpp>
2)using namespace boost;
3)progress_display(unsigned long expected_count);
4.date_time库:
1)#include <boost/date_time/gregorian/gregorian.hpp> 处理日期的组件
2)using namespace boost::gregorian;
3)#include <boost/date_time/posix_time/posix_time.hpp>处理时间的组件
4)using namespace boost::posix_time;
5)提供years、months、weeks等三个时长类。
5.日期date类:
1)date d1;
date d2(1991,8,5);
date d3(1991,Aug,5);
2)返回当前日期:
day_clock::local_day();
day_clock::universal_day();
3)year()、month()、day()分别返回date对象中年、月、日信息。
4)day_of_week()返回date对象的星期数,0为星期天,剩余的递增。
5)week_number()返回date对象时当年第几周,0~53。
6)日期输出使用三个函数,分别如下:
to_simple_string(date d):2016-Jan-06
to_iso_string(date d):20160106
to_iso_extended_string(date d):2016-01-06
7)date_duration类:表示时间持续长度,days()返回持续时间的天数。
8)日期区间date_period类:
date_period dp1(date(2016,1,6),days(20));
date_period dp2(date(2016,1,6),date(2016,1,16));
输出date_period结构体对象时,格式为[2016-Jan-06/2016-Jan-16]
9)日期区间shift()和expand():
shift()操作将时间区间整体平移n天。
expanded()操作将时间区间整体向两边各平移n天,总天数增加2n天。
10)日期迭代器:day_iterator、month_iterator、year_iterator。
date a(2016,1,6);
day_iterator d_iter(a);
++d_iter;
此时:d_iter==date(2016,1,16);
此外还可设置步长,使用起来更加方便。
6.时间处理类date_time:
1)时间长度time_duration类,提供几个子类,包括hours、minutes、seconds、millisec/milliseconds、microsec/microseconds和nanosec/nanoseconds类,但是这些类的后边几个我都没怎么用过。
2)time_duration类的使用:
time_duration td(1,10,30,1000); //这里的1000单位是微秒,1000微秒即1毫秒。
使用hours h(1);创建1个小时的长度。minutes和seconds等以此类推。
可以使用duration_from_string()构建time_duration对象,如time_duration td = duration_from_string("1:10:30:001");同时可通过hours()、minutes()、seconds()获取对应的时分秒值。time_duration支持四则运算和比较操作,处理时间十分方便。
3)date_time库默认的时间精度是微秒,若定义宏BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG时,时间精度将调整为纳秒。同时可以使用成员函数ticks_per_second()自定义毫秒单位,如将10毫秒定义为一个单位,长度为10毫秒,即0.01秒。
4)时间点ptiime类:时间长度time_duration加上一个小于一天的时间长度。
using namespace boost::gregorian;
ptime p(date(2016,1,8),hours(17));
使用time_from_string()和from_iso_string()创建时间点。
ptime p1 = time_from_string("2016-1-8 16:58:00");
ptime p2 = from_iso_string("20140108T165800");
second_clock和microsec_clock提供的local_time()和universal_time()接口返回本地当前时间和UTC当前时间。
5)ptime使用date()和time_of_day()获取time_duration中的日期和时间长度。
将ptime转换成字符串可使用to_simple_string()、to_iso_string()、to_iso_extended_string()。
6)时间区间time_period()和date_period()相似。
7)时间迭代器只有一个time_iterator,日期迭代器有多个,year_iterator、month_iterator、day_iterator。
8)使用date_facet、time_facet可定制日期时间的表现形式,将英文时间格式转换为中文日期显示格式。
9)本地时间:由于不同时区有着不同的本地时间,boost::local_time命名空间用于本地时间的使用。date_time库附带了一个CSV格式的文本数据库date_time_zonespec.csv,路径为libs/date_time/data下,方便使用。
小结:本章介绍了日期和时间库timer以及date_time的使用,提供了强大的日期时间迟滞,基本满足程序的使用。