您的位置:首页 > 其它

Boost之时间与日期处理

2015-01-19 21:18 567 查看
特点缺点说明
timer计时基类不适合大跨度时间适用大部分的普通计时
progress_timer继承自timer 可以自动写入流中只精确到0.01s如果需要更精确,可派生,退出局部域,自动调用elapsed()
progress_display图形化显示进度无法把进度显示输出与

程序显示输出分离

如果还有其他输出则会干扰进度显示。

折中的办法是重新显示 { pd.restart(size); pd+= pNum; }

date日期结构,时间点,核心类——支持日期运算
date_duration时间长度——表示一段时间,可以把它看成一个int
date_period标量,左开右闭,时间区间——左闭右开,可以认为是一个有起点的date_duration。能做交集、并集
date_iterator迭代器,以某个单位增减——ady_iterator,week_iterator,month_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/progress.hpp>
#include<iostream>
using namespace boost;
using namespace std;

int main()
{
progress_timer t;

cout<<"max timespan:"
<<t.elapsed_max()/3600<<"h"<<endl;
cout<<"min_timespan:"
<<t.elapsed_min()<<"s"<<endl;
{
progress_timer t1;
}
{
progress_timer t2;
for(int i=0;i<100;i++);
}
}


#include<boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
using namespace std;

int main()
{
date d(2008,11,20);

date d18years = d + years(18);
cout << d18years <<" is "
<<d18years.day_of_week()<<endl;

int count=0;

for( day_iterator d_iter(date(d18years.year(),11,1)) ;
d_iter != d18years.end_of_month();++d_iter)
{
if(d_iter->day_of_week()==Sunday)
{
++count;
}
}

cout<< "total "<<count<<"Sunday."<<endl;

count=0;
for(month_iterator m_iter(date(d18years.year(),1,1));
m_iter<date(d18years.year()+1,1,1);++m_iter)
{
count+=m_iter->end_of_month().day();
}

cout<< "total "<<count<<"days of year."<<endl;
}


#include<boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
using namespace std;

int main()
{
date d1(2000,1,1),d2(2008,8,8);
cout<<d2-d1<<endl;
assert(d1+(d2-d1)==d2);

d1+=days(10);
assert(d1.day()==11);
d1+=months(2);
assert(d1.month()==3&&d1.day()==11);
d1-=weeks(1);
assert(d1.day()==4);

d2-=years(7);
assert(d2.year()==d1.year()+1);

date d3(2010,1,1);

date d4=d3+days(pos_infin);
assert(d4.is_pos_infinity());

d4=d3+days(not_a_date_time);
assert(d4.is_not_a_date());
d4=date(neg_infin);
days dd= d3-d4;
assert(dd.is_special()&&!dd.is_negative());
}


#include<vector>
#include<fstream>
#include<boost/progress.hpp>
using namespace boost;
using namespace std;

int main()
{
std::vector<std::string> v(100);
std::ofstream fs("./test.txt");
progress_display pd(v.size());
for(vector<string>::iterator x=v.begin();x!=v.end();x++)
{
fs<<*x<<std::endl;
++pd;
}
}
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
int main()
{
//boost::gregorian::date d1=from_string("1999-12-31");
boost::gregorian::date d2=from_undelimited_string("20011118");
std::cout<<day_clock::local_day()<<std::endl;
std::cout<<day_clock::universal_day()<<std::endl;

date d(2015,1,20);
//std::cout<<to_simple_string(d)<<std::endl;
//std::cout<<to_iso_string(d)<<std::endl;
//std::cout<<to_iso_extended_string(d)<<std::endl;
std::cout<<d<<std::endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: