1. timer
计时器timer:小型计时器,提供毫秒级别的计时
#include <boost/timer.hpp>
#include <iostream>
using namespace boost;
int main()
{
timer t; //声明计时器,开始计时
std::cout << "max_timespan:" //可度量最大时间
<< t.elapsed_max() / 3600 << "h" << std::endl;
std::cout << "min_timespan:" //可度量最小时间
<< t.elapsed_min() << "s" << std::endl;
std::cout << "now_since_time:" //自计时器启动所流逝的时间
<< t.elapsed() << std::endl;
}
timer源码:
std::clock()返回自进程启动以来的clock数(节拍),每秒的clock数由CLOCKS_PER_SEC定义,windows为1000(毫秒),Linux为1000000(微秒)。
生成一个timer对象时,启动构造函数记录此时的clock,然后每次操作会获取当前操作时的clock数,(当前操作时的clock数-开始时的clock数) / CLOCKS_PER_SEC。
最小精度:1/CLOCKS_PER_SEC
最大计时:通过numeric_limits获得clock_t类型的最大值,然后同上。
总结:不适用高精度计时场景,且精度受平台影响;也不适用于时间跨度大的场景(年月日等);无析构函数。
progress_timer
2. progress_timer
派生自timer,具有timer所有功能,并增加了析构函数,当其析构时可自动输出时间(看作progress_timer对象解析时自动输出从生成到析构之间的时间),其构造函数中:
progress_timer(std::ostream& os);
析构时输出定向到I/O流,默认std::cout。
3. progress_display-进度指示器
(与timer、progress_timer无联系)
progress_display(unsigned long expected_count); //构造函数:参数-用于进度显示的基数
#include <iostream>
#include <boost/progress.hpp>
#include <vector>
#include <fstream>
using namespace boost;
using namespace std;
int main()
{
vector<string> v(1000,"a");
ofstream fs("./test.txt");
progress_display pd(v.size());
for (auto& x : v)
{
fs << x << endl;
++pd; //增加计数count,count/expected_count表示进度百分比
}
}
4. date_time
基本知识:date_time:gregorian(处理日期)+posix_time(处理时间)
时间点:时间轴上的一个点
时间段:两个时间点之间的一个区间
时长:两个时间点之差(标量)
处理年月日、基于格里高利历、支持1400-01-01~9999-12-31
//创建日期对象操作:
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
using namespace std;
int main()
{
//创建一般日期
date d1; cout << "d1=" << d1 << endl;
date d2(2021, 8, 9); cout << "d2=" << d2 << endl;
date d3(2021, Aug, 1); cout << "d3=" << d3 << endl;
date d4(d2); cout << "d4=" << d4 << endl;
//比较操作函数
assert(d1 == date(not_a_date_time));
assert(d2 == d4);
assert(d3 < d4);
//创建特殊日期
date d5(neg_infin); cout << "d5=" << d5 << endl;
date d6(pos_infin); cout << "d6=" << d6 << endl;
date d7(not_a_date_time); cout << "d7=" << d7 << endl;
date d8(max_date_time); cout << "d8=" << d8 << endl;
date d9(min_date_time); cout << "d9=" << d9 << endl;
//截取字符串创建日期
date d10 = from_string("1999-12-31"); cout << "d10=" << d10 << endl; //连字符
date d11(from_string("2015/1/1")); cout << "d11=" << d11 << endl; //斜杠
date d12 = from_undelimited_string("20011118"); cout << "d12=" << d12 << endl; //无分隔符
cout << "local_day=" << day_clock::local_day() << endl;
cout << "universal_day=" << day_clock::universal_day() << endl;
}
//访问日期操作:
//day()、month()、year()获取日期
cout << "year=" << d1.year() << endl;
cout << "month=" << d1.month() << endl;
cout << "day=" << d1.day() << endl;
//获取year_month_day()返回结构成员
date::ymd_type ymd = d1.year_month_day();
cout << "year=" << ymd.year << endl;
cout << "month=" << ymd.month << endl;
cout << "day=" << ymd.day << endl;
//参数日期的星期数、当年第几天、当月最后一天、当年第几周
cout << d1.day_of_week() << endl;
cout << d1.day_of_year() << endl;
cout << d1.end_of_month() << endl;
cout << d1.week_number() << endl;
//日期输出格式
cout << to_simple_string(d1) << endl; //YYYY-mmm-DD:月份为英文
cout << to_iso_string(d1) << endl; //YYYYMMMDD:数字字符串
cout << to_iso_extended_string(d1) << endl; //YYYY-MM-DD:数字字符串
cout << d1 << endl; //默认://YYYY-mmm-DD:月份为英文
//转换C格式tm:
date d1(2021, 8, 9);
tm t = to_tm(d1);
cout << t.tm_hour << endl; /* Hours. [0-23] */
cout << t.tm_min << endl; /* Minutes. [0-59] */
cout << t.tm_sec << endl; /* Seconds. [0-60] (1 leap second) */
cout << t.tm_year << endl; /* Year - 1900. */
cout << t.tm_mon << endl; /* Month. [0-11] */
cout << t.tm_mday << endl; /* Day. [1-31] */
cout << t.tm_wday << endl; /* Day of week. [0-6] */
cout << t.tm_yday << endl; /* Days in year.[0-365] */
date d2 = date_from_tm(t); //只转换tm_year、tm_mon、tm_mday
cout << d2 << endl;
//日期长度操作
days dd1(10), dd3(321);
date_duration dd2(-5);
cout << dd1.days() << endl;
cout << dd1.days()/2 << endl;
cout << dd2.days() << endl;
weeks w1(3);
cout << w1.days() << endl;
months m1(2);
cout << m1.number_of_months() << endl;
years y1(3);
cout << y1.number_of_years() << endl;
months m2 = m1 + y1;
cout << m2.number_of_months() << endl;
//日期运算
date d1(2000, 1, 1), d2(2017, 11, 18);
cout << d2 - d1 << endl;
cout << d1 << endl;
d1 += days(10);
cout << d1 << endl;
d1 += months(2);
cout << d1 << endl;
d1 += weeks(2);
cout << d1 << endl;
d1 += years(3);
cout << d1 << endl;
/*日期区间:date_period
**1、指定区间两端点(左闭右开)
**2、左端点+时长 */
date_period dp1(date(2020, 1, 9), date(2021, 5, 6));
date_period dp2(date(2021, 8, 7), days(60));
cout << dp1 << endl;
cout << dp1.begin() << endl;
cout << dp1.last().day() << endl;
cout << dp1.end().day() << endl; //last+1
cout << dp1.length() << endl;
cout << dp2 << endl;
dp2.shift(days(3)); //在时间轴上整体左移3天
cout << dp2.begin() << " " << dp2.length() << endl;
dp2.expand(days(10)); //在时间轴上向两端各扩展10天
cout << dp2.begin() << " " << dp2.length() << endl;
date_period dp3(date(2000, 1, 1), days(20));
cout << dp3.is_after(date(1900, 1, 1)) << endl; //bool:是否在之后
cout << dp3.is_before(date(2001, 1, 1)) << endl; //bool:是否在之前
cout << dp3.contains(date(2000, 1, 5)) << endl; //bool:是否包含
date_period dp4(date(2000, 1, 1), days(10));
cout << dp3.contains(dp4) << endl; //bool:是否包含
cout << dp3.intersects(dp4) << endl; //bool:是否交集
cout << dp3.intersection(dp4) << endl; //period:交集区间
cout << dp3.is_adjacent(dp4) << endl; //bool:是否相邻
//并集操作
date_period dp5(date(2000, 1, 22), days(20));
cout << dp4.merge(dp5) << endl; //有交集返回合并区间,否则返回无效区间
cout << dp4.span(dp5) << endl; //合并区间(两者间间隔也合并)
//日期迭代器
date d(2000, 1, 1);
day_iterator d_iter(d); //默认步长为1
++d_iter;
cout << d_iter->day() << endl;
year_iterator y_iter(d,2); //默认步长为1
++y_iter;
cout << y_iter->year() << endl; //2002
cout << *y_iter << endl; //2002.1.1
***posix_time***处理时间,微秒级别(纳秒:定义宏)
time_duration类:时间长度
ptime类:时间点
//操作时间长度
time_duration td(1, 10, 30, 2000); //时、分、秒、微秒
hours h(1);
minutes m(10);
seconds s(30);
millisec ms(2000); //毫秒
microsec mss(2000); //微秒
time_duration td1 = h + m + s + ms + mss;
time_duration td2 = hours(1) + minutes(10) + seconds(30) + millisec(2000) + microsec(2000);
time_duration td3 = duration_from_string("1:10:30:001"); //时、分、秒、微秒(0-999)
cout << td << endl;
cout << td1 << endl;
cout << td2 << endl;
cout << td3 << endl;
hours h1(-10);
cout << h1.is_negative() << endl;
cout << h1.invert_sign() << endl;
cout << to_simple_string(td2) << endl; //HH:MM:SS.ffffff
cout << to_iso_string(td2) << endl; //HHMMSS.ffffff
//时间长度的精度:date_time库默认精度是微秒,秒以下的时间度量使用微秒
time_duration nt(1, 10, 20, 1000); //时、分、秒、纳秒=0.001微秒=0.000001毫秒
cout << nt << endl;
cout << nt.fractional_seconds() << endl; //秒的小数部分,以时间精度为单位
cout << nt.unit() << endl; //最小单位
cout << nt.resolution() << endl; //时间长度分辨率(枚举值)
cout << nt.num_fractional_digits() << endl; //秒的小数部分:微秒6位,纳秒9位
//时间点ptime
ptime p(date(2021, 8, 15), hours(1)); //一个日期加一个时间偏移量
ptime p1 = time_from_string("2021-8-15 01:00:00"); //从字符串构造
ptime p2 = from_iso_string("20210815T010000");
ptime p3 = second_clock::local_time(); //秒级分辨率,本地当前时间
ptime p4 = microsec_clock::universal_time(); //微秒级分辨率,UTC当前时间
//操作时间点对象
ptime p(date(2021, 8, 15), hours(1)+minutes(30)); //date+time_duration
date d = p.date();
time_duration td = p.time_of_day();
cout << p << endl;
cout << d << endl;
cout << td << endl;
cout << p.date().month() << endl;