您的位置:首页 > 其它

boost学习2.7:处理时间

2016-01-18 15:38 447 查看
[b](1)时间长度[/b]

[b][b](2)时间长度的精度[/b][/b]

[b][b][b](3)时间点[/b][/b][/b]

[b][b][b][b](4)创建时间点对象[/b][/b][/b][/b]

[b][b][b][b](5)操作时间点对象[/b][/b][/b][/b]

[b][b][b][b][b](6)与tm,time_t等结构的转换[/b][/b][/b][/b][/b]

[b][b][b][b](7)时间区间[/b][/b][/b][/b]

[b][b][b][b][b](8)时间迭代器[/b][/b][/b][/b][/b]

[b][b][b][b](9)综合运用。[/b][/b][/b][/b]

需要包含库文件和命名空间

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


相当于在年月日下加入了时分秒的分辨率

(1)时间长度

类:time_duration

操作时间长度:

//时间长度类对象的初始化
time_duration t1(1,10,30,1000);//1小时10分30秒1毫秒(1000微秒)
hours h(1);    //小时
minutes m(1);//分
seconds s(1);//秒
millisec mi(1);    //毫秒
t1=h+m+s+mi;//
t1=hours(1)+minutes(2)//直接时分秒相加而得
t1=duration_from_string("1:10:20:001");//使用工厂函数


time_duration t1(1,10,30,1000);//1小时10分30秒1毫秒(1000微秒)
cout<<t1.hours()<<endl;//返回当前是几小时
cout<<t1.minutes()<<endl;//返回当前是几分
cout<<t1.seconds()<<endl;//返回当前是几秒
cout<<t1.total_seconds()<<endl;//返回总共的秒数
cout<<t1.total_milliseconds()<<endl;//返回总共的毫秒数
cout<<t1.total_microseconds()<<endl;//总共微秒数
cout<<t1.fractional_seconds()<<endl;


(2)时间长度的精度

date_time的默认精度是微秒

当定义如下的宏时,精确度将为纳秒:

#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG

time_duration t1(1,10,30,1000);//1小时10分30秒1毫秒(1000微秒)
cout<<t1<<endl;
cout<<t1.resolution()<<endl;//返回时间长度的分辨率
cout<<t1.num_fractional_digits()<<endl;//6,小数部分的位数


(3)时间点

ptime:核心类

class ptime : public date_time::base_time<ptime, posix_time_system>
{
public:
typedef posix_time_system time_system_type;
typedef time_system_type::time_rep_type time_rep_type;
typedef time_system_type::time_duration_type time_duration_type;
typedef ptime time_type;
//! Construct with date and offset in day
ptime(gregorian::date d,time_duration_type td) : date_time::base_time<time_type,time_system_type>(d,td)
{}
//! Construct a time at start of the given day (midnight)
explicit ptime(gregorian::date d) : date_time::base_time<time_type,time_system_type>(d,time_duration_type(0,0,0))
{}
//! Copy from time_rep
ptime(const time_rep_type& rhs):
date_time::base_time<time_type,time_system_type>(rhs)
{}
//! Construct from special value
ptime(const special_values sv) : date_time::base_time<time_type,time_system_type>(sv)
{}
#if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
// Default constructor constructs to not_a_date_time
ptime() : date_time::base_time<time_type,time_system_type>(gregorian::date(not_a_date_time), time_duration_type(not_a_date_time))
{}
#endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR

};


(4)创建时间点对象

需包含的库以及命名空间

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
//#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
#define BOOST_DATE_TIME_SOURCE
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace std;;
using namespace boost;


ptime t(date(2016,1,19),hours(11));
cout<<t<<endl;
//获得本地时间
ptime t1=second_clock::local_time();//获得本地时间,秒的分辨率
cout<<t1<<endl;
ptime t2=microsec_clock::universal_time();//UTC当前时间,微秒的分辨率
cout<<t2<<endl;


(5)操作时间点对象

date()和time_of_day()获得时间点中的日期和时间长度

ptime t1=second_clock::local_time();//获得本地时间,秒的分辨率
cout<<t1.date()<<endl;
cout<<t1.time_of_day()<<endl;


ptime t1=second_clock::local_time();//获得本地时间,秒的分辨率
cout<<t1.date()<<endl;
cout<<t1.time_of_day()<<endl;
这里链接出现了问题,好像我这个里面的所有和字符串相关的时间转换操作都会出现连接错误???
cout<<boost::gregorian::to_simple_string(t1)<<endl;


(6)与tm,time_t等结构的转换

ptime t1=second_clock::local_time();//获得本地时间,秒的分辨率
tm t=to_tm(t1);


(7)时间区间

time_period:时间区间类

与date_period类相类似

ptime t1(date(2000,1,1),hours(1));
ptime t2(date(2000,1,1),hours(3));
time_period tp(t1,t2);
cout<<tp.begin()<<endl;
cout<<tp.end()<<endl;


(8)时间迭代器

这里这有一个迭代器time_iterator,定义时传入时间ptime和一个时长(作为步长)

ptime t1(date(2000,1,1),hours(1));
for(time_iterator i(t1,minutes(10));i<t1+hours(10);i++)
{
cout<<*i<<endl;
}


(9)综合运用

高精度计时器

一:不采用模板,直接写的

class basic_ptimer
{
private:
ptime _start_time;
public:
void restart();
void elapsed()const;

basic_ptimer()//构造函数,得到当前的时间
{
restart();
}
~basic_ptimer()//最后析构函数,得到当前的时间减去之前时间
{
elapsed();
}

};
void basic_ptimer::restart()
{
_start_time=microsec_clock::local_time();
}
void basic_ptimer::elapsed()const
{
cout<<microsec_clock::local_time()-_start_time<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
basic_ptimer t;

for(int i=0;i<1000;i++)
cout<<" ";
cout<<endl;
}


二:采用模板进行重写(即可以设置不同的精度)

template<typename clock=microsec_clock>//缺省使用microsec_clock
class basic_ptimer
{
private:
采用模板的时候,不知道为什么,当把这两个成员函数,放在类体外面的时候,就报错,不晓得为啥???
ptime _start_time;
void restart()
{
_start_time=clock::local_time();
}
void elapsed()
{
cout<<clock::local_time()-_start_time<<endl;
}
public:

basic_ptimer()//构造函数,得到当前的时间
{
restart();
}
~basic_ptimer()//最后析构函数,得到当前的时间减去之前时间
{
elapsed();
}
};
typedef basic_ptimer<microsec_clock> ptimer;//用typedef将模板类实例化。
typedef basic_ptimer<second_clock> sptimer;
int _tmain(int argc, _TCHAR* argv[])
{
ptimer t;

for(int i=0;i<1000;i++)
cout<<" ";
cout<<endl;
}


计算工作时间:

在每次运行时,得到目前的时间,输出对应的问候语。

#include "stdafx.h"
#include<stdlib.h>
#include <stdio.h>
#include<map>
#include <string>
#include<iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
#define BOOST_DATE_TIME_SOURCE
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace std;;
using namespace boost;

class work_time
{
public:
typedef map<time_period,string> map_t;
private:
map_t map_ts;//将时间段与这个期间的问候语关联起来(map)
void init()
{
ptime p(day_clock::local_day());//
map_ts[time_period(p,hours(9))]="还没到9点";
p+=hours(9);
map_ts[time_period(p,hours(3)+minutes(30))]="上午上班时间";
p+=(hours(3)+minutes(30));
map_ts[time_period(p,hours(1))]="吃午饭时间";
p+=hours(1);
map_ts[time_period(p,hours(4)+minutes(30))]="下午上班时间";
p+=(hours(4)+minutes(30));
map_ts[time_period(p,hours(6))]="晚上休息时间";
}
public:
work_time()//构造函数,调用init函数,初始化map_ts
{
init();
}
public://在得到一个时间t后,遍历map后就可以输出对应问候语
void greeting(const ptime& t)
{
map_t::iterator i;
for(i=map_ts.begin();i!=map_ts.end();++i)
if((i->first).contains(t))
{
cout<<i->second<<endl;
break;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
work_time t1;
t1.greeting(second_clock::local_time());
}


greeting函数重写(1)C++11

[b]greeting函数重写(2)C++11[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: