boost -日期与时间相关库

1.timer类:

#include "stdafx.h"

#include <boost/timer.hpp>
#include <iostream>
using namespace std;
using namespace boost;


int _tmain(int argc, _TCHAR* argv[])
{
timer t;
cout<<"max timespan:"<<t.elapsed_max()/3600<<endl;//可度量最大时间,小时为单位
cout<<"min timespan:"<<t.elapsed_min()<<endl;//可度量最小时间,以秒为单位
cout<<"not time elapsed"<<t.elapsed()<<endl;//自timer对象创建后流逝的时间 
return 0;
}


2.boost::progress_timer t类

自动打印输出流逝的时间

#include <boost\progress.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
boost::progress_timer t;
return 0;
}

3.new_progress_timer 继承boost::progress_timer的自定义类,可自定义精度

#include "stdafx.h"
#include <boost/timer.hpp>
#include <boost\progress.hpp>
#include <boost\static_assert.hpp>
#include <iostream>
using namespace std;


template<int N=2>
class new_progress_timer:public boost::timer
{
public:
new_progress_timer(std::ostream&os = std::cout)
:m_os(os)
{
BOOST_STATIC_ASSERT(N>=0&&N <=10);
}
~new_progress_timer(void)
{
try
{
std::istream::fmtflags old_flags = m_os.setf(std::istream::fixed,std::istream::floatfield);
std::streamsize old_prec = m_os.precision(N);
m_os<<elapsed()<<" s\n"<<std::endl;


//恢复流状态
m_os.flags(old_flags);
m_os.precision(old_prec);
}
catch(...)
{}
}
private:
std::ostream & m_os;
};
int _tmain(int argc, _TCHAR* argv[])
{
new_progress_timer<6> tm;
int i=0;
while(i<100)
{
i++;
cout<<i<<endl;
}
return 0;
}

4.progress_display类:可以在控制台上显示程序的执行进度

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

int _tmain(int argc, _TCHAR* argv[])
{
vector<std::string> v(100);
//ofstream fs("./test.txt");
//声明一个progress_display对象,基数是v的大小
boost::progress_display pd(v.size());
//开始迭代遍历向量,处理字符串,写入文件
vector<std::string>::iterator itor = v.begin();
while(itor!=v.end())
{
//fs<<*itor<<endl;
++pd;
itor++;
}
return 0;
}


5.date类 表示年月日的日期类 以天为单位表示时间点概念

简单构造

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

date d1; //一个无效的日期
date d2(2010,1,1);//使用数字构造日期
date d3(2010,Jan,1);//也可以使用英文制定月份
date d4(d2); //date之初拷贝构造


assert(d1 == date(not_a_date_time));//not_a_date_time代表无效日期
assert(d2 == d4);//datetime支持比较操作
assert(d3< d4);

工厂函数构造date

from_string() 、from_undelimited_string、day_clock::local_day()、day_clock::universal_day()

调用示例:

date d1 =from_string("1999-12-31");
date d2(from_string("2005/1/1"));
date d3 = from_undelimited_string("20111118");

date d1 =from_string("1999-12-31");
date d2(from_string("2005/1/1"));
date d3 = from_undelimited_string("20111118");


date d4 = day_clock::local_day();//返回本地日期
date d5 = day_clock::universal_day();//返回UTC日期


6.特殊日期

date d1(neg_infin); //负无限日期
date d2(pos_infin); //正无限日期
date d3(not_a_date_time);//无效日期
date d4(max_date_time);//最大可能日期 9999-12-31
date d5(min_date_time);//最小可能日期1400-01-01

7.访问日期

date d(2010,4,1);
assert(d.year()==2010);//返回年
assert(d.month()==4);//返回月
assert(d.day()==1);//返回日
date::ymd_type ymd = d.year_month_day();//一次性获得年月日数据
assert(d.day_of_week()==4);//返回date的星期数
assert(d.day_of_year() == 91);//返回date是当年的第几天
assert(d.end_of_month() == date(2010,4,30));//返回当月最后一天的date对象
assert(date(2010,1,10).week_number() ==1);//返回date所在的周是当年的第几周,范围是0至53,如果年初的几天位于去年的周那么周数为53,即第0周

assert(date(pos_infin).is_infinity());//是否是一个无限日期
assert(date(pos_infin).is_pos_infinity());//是否是一个负无限日期
assert(date(neg_infin).is_neg_infinity());//是否是一个正无限日期
assert(date(not_a_date_time).is_not_a_date());//是否是一个正无限日期
assert(date(not_a_date_time).is_special());//是否是一个无效日期
assert(!date(2010,10,1).is_special());//是否是任意一个特殊日期

8.日期的输出

to_simple_string(date d);//转换为YYYY-mmm-dd格式的字符串,其中mmm为3字符的英文月份名
to_iso_string(date d);//转换为YYYYMMDD格式的数字字符串
to_iso_extended_string(date d);//转换为YYYY-MM-DD格式的数字字符串

9.与TM结构的转换

to_tm(date):date转换到tm,tm的时分秒成员(tm_hour,tm_min,tm_sec)均置为0,夏令时标志tm_isdst置为-1(表示未知).

date_from_tm(tm datetm): tm转换到date。只使用年、月、日三个成员(tm_year,tm_mon,tm_mday),其他成员均被忽略。

示例:

date d(2010,2,1);
tm t = to_tm(d);
assert(t.tm_hour ==0 && t.tm_min ==0);
assert(t.tm_year == 110 && t.tm_mday ==1);
date d2 = date_from_tm(t);
assert(d == d2);

10,日期长度(date_duration)

支持 + -  / 运算,并支持(== 、!= 、<、<=等)运算

datetime库为date_duration定义了常用的宏:typedef:days

days示例:

days dd1(10),dd2(-100),dd3(225);
assert(dd1>dd2 && dd2<dd3);
assert(dd1 + dd2 == days(-90));
assert((dd2 +dd3).days() == 265);
assert(dd3/5 == days(51));

month、years、weeks示例

weeks w(3); //三个星期
assert(w.days() ==21);
months m(5);//5个月
years y(2);//2年
month m2 = y +m;//2年零5个月
assert(m2.number_of_months() == 29);
assert((y*2).number_of_years() ==4);

11.日期运算


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值