松阳 通过本文主要向大家介绍了c++ boost,c++ boost库,boost c++ libraries,boost,boost升压 电感参数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
boost 时间与日期处理
导视:
类</div> | 特点</div> | 缺点</div> | 说明 |
timer</div> | 计时基类</div> | 不适合大跨度时间</div> | 适用大部分的普通计时 |
progress_timer</div> | 继承自timer 可以自动写入流中</div> | 只精确到0.01s</div> | 如果需要更精确,可派生个类,调用stream的precision设置 |
progress_display | 图形化显示进度 | 只能输出到cout | 如果还有其他输出则会干扰进度显示。 折中的办法是重新显示 pd.restart(size); pd+= pNum; |
date | 日期结构,时间点 | —— | date是date_time库的核心类 boost::gregorian |
date_duration | days、months、years 时间段 | —— | 表示一段时间,可以把它看成一个int |
date_period | 标量,左开右闭,时间区间 | —— | 可以认为是一个有起点的date_duration。能做交集、并集 |
date_iterator | 迭代器,以某个单位增减 | —— | 天、周、月、年四种迭代器,以某种增量移动。 |
time_duration | 时间段 同date_duration | —— | hours、minutes、seconds、millisec、boost::posix_time |
ptime | 时间点 date+time_duration | —— | 分date()和time_of_day()操作。 |
time_period | 时间区间 同date_period | —— | —— |
time_iterator | 迭代器,以某个单位增减 | —— | 可直接与ptime比较 |
date_facet | 流格式化日期 | —— | %Y年%m月%d日 |
time_facet | 流格式化时间 | —— | %Y年%m月%d日 %H点%M分%S%F秒 |
#include <boost/timer.hpp> #include <boost/progress.hpp> #include <iostream> #include <sstream> #include <fstream> #include <string> #include <vector> #include <Windows.h> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> using namespace std; int main() { boost::timer t; std::cout<<"Max "<<t.elapsed_max()<<endl; std::cout<<"Min "<<t.elapsed_min()<<endl; std::cout<<"elapsed: "<<t.elapsed()<<endl; t.restart(); Sleep(100); std::cout<<"elapsed: "<<t.elapsed()<<endl; cout<<"---------------------------"<<endl; stringstream ss; { boost::progress_timer t(ss); Sleep(300); } cout<<ss.str(); cout<<"---------------------------"<<endl; vector<string> v(100); //Do Data Fill...... ofstream fs("c:\test.txt"); boost::progress_display pd(v.size()); vector<string>::iterator pos; for (pos = v.begin();pos != v.end();++pos) { fs<<*pos<<endl; Sleep(10); ++pd; //pd.restart(v.size()); //pd+=(pos-v.begin() +1); } cout<<"---------------------------"<<endl; { using namespace boost::gregorian; cout<<"----------------- date ------------------"<<endl; date d1; date d2(2013,4,7); date d3(2013,Apr,7); date d4(d2); assert(d1 == date(not_a_date_time)); //默认初始化为无效日期 assert(d2 == d4); assert(d3 == d2); d1 = from_string("1999,9,9"); date d5 (from_string("2008/8/8")); d3 = from_undelimited_string("20110111"); cout<<day_clock::local_day()<<endl; cout<<day_clock::universal_day()<<endl; date d6 (neg_infin); date d7(pos_infin); cout<<d6<<endl; cout<<d7<<endl; cout<<"---------------------------"<<endl; date today (2013,4,17); assert(today.year() == 2013); assert(today.month() == 4); assert(today.day() == 17); date::ymd_type ymd = today.year_month_day(); assert(ymd.year == 2013); assert(ymd.month == 4); assert(ymd.day == 17); assert(today.day_of_week() == 3); //星期几 周日为0 cout<<today.day_of_year()<<endl; //在一年中是第几天 assert(today.end_of_month() == date(2013,4,30)); //当月的最后一天 cout<<today.week_number()<<endl; //当年的第几周 范围0~53 年初的半周归为上一年,即53 assert(d6.is_infinity()); //日期为无限日期 assert(d6.is_neg_infinity()); cout<<"---------------------------"<<endl; cout<<to_simple_string(today)<<endl; cout<<to_iso_string(today)<<endl; cout<<to_iso_extended_string(today)<<endl; //常用日期格式YYYY-MM-DD cout<<today<<endl; cout<<"---------------------------"<<endl; tm t = to_tm(today); assert(t.tm_hour == 0 && t.tm_min == 0); date new_today = date_from_tm(t); //从tm转为date assert(new_today == today); cout<<"-------------- days(date_duration) --------------"<<endl; days dd1(10),dd2(-20),dd3(365); assert(dd1>dd2 &&dd1<dd3); assert(dd1+dd2 == days(-10)); assert((dd2+dd3).days() == 345); assert(dd3/5 == days(73)); weeks w(3); //3个星期 assert(w.days() == 21); months m(5); years y(2); months m2 = y+m; assert(m2.number_of_months() == 29); assert((y*2).number_of_years() == 4); cout<<"-------------- Calc --------------"<<endl; date dA(2000,1,1),dB(2008,8,8); cout<<dB-dA<<endl; //3142天 dA+=days(10); assert(dA.day() == 11); dA+=months(2); assert(dA.month() ==3 && dA.day()== 11); dA-=weeks(1); assert(dA.day() == 4); dB-=years(7); assert(dA.year() == dB.year()-1); //如果日期是月末的最后一天,加减月或年会得到月末的时间,而不是简单的月、年加1 date sp(2013,3,30); sp-=months(1); assert(sp.month() == 2 && sp.day() == 28); sp -=months(1); assert(sp.month()== 1 && sp.day()== 31); sp+=months(2); assert(sp.day() == 31); //与原来的日期已经不相等! cout<<"-------------- date_period --------------"<<endl; date_period dp(date(2013,4,17),days(14)); //左开右闭与STL的容器相似 assert(!dp.is_null()); assert(dp.begin().day() == 17); assert(dp.last().day() == 30); assert(dp.end().day() == 1); cout<<dp<<endl; date_period new_dp = dp;