《BOOST程序库完全开发指南》 第02章 时间与日期

记时器 timer:

#include <iostream>
#include <boost/timer.hpp>

int main()
{
    boost::timer t; //声明一个记时器对象,声明对象时即开始记时
    std::cout<<t.elapsed_max()<<std::endl; //可度量的最大时间,以h为单位
    std::cout<<t.elapsed_min()<<std::endl; //可度量的最小时间,以s为单位
    std::cout<<t.elapsed()<<std::endl; //声明对象到现在经过的时间
    t.restart(); //重新记时
    std::cout<<t.elapsed()<<std::endl; //重新记时后到现在经过的时间
}


记时器 progress_timer:

#include <iostream>
#include <boost/progress.hpp>

int main()
{
    boost::progress_timer t;    //声明一个记时器对象,声明对象时即开始记时
    std::cout<<t.elapsed_max()<<std::endl; //可度量的最大时间,以h为单位
    std::cout<<t.elapsed_min()<<std::endl; //可度量的最小时间,以s为单位
    std::cout<<t.elapsed()<<std::endl; //声明对象到现在经过的时间
    t.restart(); //重新记时
    std::cout<<t.elapsed()<<std::endl; //重新记时后到现在经过的时间
}

用法完全同于 timer,唯一的不同是 progress_timer 在析构函数中,会自动调用一次 elapsed() 方法记时,所以这里会输出三次记时。所以如果只有一次记时,可以简单的只声明对象即可。(注意作用域)另外还有一个不同点,它的输出精度为小数点后两位,只精确到百分之一秒。而 timer 的精度在win32下是毫秒(1/1000s),在 linux 下是微秒(1/1000000)。

进度条显示 progress_display:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <boost/progress.hpp>

int main()
{
    std::vector<std::string> vect(1000000);
    std::ofstream ofs("test.log");
    boost::progress_display pd(vect.size());
    std::vector<std::string>::iterator pos;
    for (pos = vect.begin();pos != vect.end(); ++pos)
    {
        ofs<<*pos<<std::endl;
        ++pd; //更新进度显示
    }
    std::cout<<"OK"<<std::endl;
}

作用不大,略。

date_time 库中用枚举 special_values 定义了一些特殊的时间概念,位于命名空间 boost::date_time,并被using语句引入其它子命名空间:

pos_infin:正无限

neg_infin:负无限

not_a_date_time:无效时间

min_date_time:可表示的最小日期或时间

max_date_time:可表示的最大日期或时间

日期处理 date

date_time 库的日期基于格里高利历,支持从 1400-01-01 到 9999-12-31之间的日期计算,位于命令空间 boost::gregorian,为了使用 date_time库的日期功能,需要包含头文件 <boost/date_time/gregorian/gregorian.hpp>

date 是一个轻量级对象,很小,处理效率很高,可以被拷贝传值,全面支持比较操作和流输入输出,可以当成基本类型来使用。

输出当前时间,如:2012-May-02 03:16:26

#include <iostream>
#include <boost/date_time.hpp>

int main()
{
    boost::posix_time::ptime p = boost::posix_time::second_clock::local_time();
    std::cout<<p<<std::endl;
}
原文地址:https://www.cnblogs.com/tianyajuanke/p/2724535.html