《Boost程序庫徹底開發指南》讀書筆記-日期時間

●timer庫

#include <boost\timer.hpp>
#include <boost\progress.hpp>

一、timer類函數

// timer類的示例。
void Lib_Demo_timer::Demo_timer()
{
    timer t;

    cout << "可度量的最大單位:" << t.elapsed_max() / 3600 << "小時" << endl;
    cout << "可度量的最小單位:" << t.elapsed_min() << "s" << endl;
    cout << "計時開始...按任意鍵計時" << endl;
    system("pause");
    cout << "已通過的時間:" << t.elapsed() << "s" << endl;
}

輸出:spa

可度量的最大單位:596.523小時
可度量的最小單位:0.001s
計時開始...按任意鍵計時
請按任意鍵繼續. . .
已通過的時間:0.74s
請按任意鍵繼續. . .操作系統

二、process類code

// progress類的示例。
void Lib_Demo_timer::Demo_process(void)
{
    {
        boost::progress_timer t;
        cout << "須要計時的代碼塊1" << endl;
        system("pause");
    }

    stringstream ss;
    {
        boost::progress_timer t(ss);
        cout << "須要計時的代碼塊2" << endl;
        system("pause");
    }
    cout << ss.str() << endl;
}

輸出:對象

須要計時的代碼塊1
請按任意鍵繼續. . .
0.96 sblog

須要計時的代碼塊2
請按任意鍵繼續. . .
1.66 sci


請按任意鍵繼續. . .字符串

三、progress_display類generator

// progress_display類的示例。
void Lib_Demo_timer::Demo_progress_display(void)
{
    vector<string> v(100);
    
    progress_display pd(v.size());

    for(vector<string>::const_iterator i = v.begin(); i != v.end(); ++i)
    {
        //針對i的處理
        Sleep(100);

        ++pd;
    }

}

輸出:源碼

0%   10   20   30   40   50   60   70   80   90   100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************
請按任意鍵繼續. . .

●date_time庫

一、引用庫的方式

(1)、包含源碼的方式:經過啓用宏 BOOST_DATE_TIME_SOURCE 等。

#define BOOST_DATE_TIME_SOURCE
#define BOOST_DATE_TIME_NO_LIB

#include <libs/date_time/src/gregorian/greg_names.hpp>
#include <libs/date_time/src/gregorian/date_generators.cpp>
#include <libs/date_time/src/gregorian/greg_month.cpp>
#include <libs/date_time/src/gregorian/greg_weekday.cpp>
#include <libs/date_time/src/gregorian/gregorian_types.cpp>

注意:若是使用的boost版本較高,那麼在使用(較低版本)STLport標準庫時會編譯錯誤,解決方法是使用VC的標準庫。

(2)、包含已編譯庫的方式

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

須要在項目配置的「庫目錄」中增長boost編譯庫的路徑。

二、定義日期對象 date

頭文件

#include <boost/date_time/gregorian/gregorian.hpp>
 // date 初始化示例
    boost::gregorian::date d1;        //默認建立一個 無效日期
    //宏 DATE_TIME_NO_DEFAULT_CONSTRUCTOR 能夠禁用 構造無效日期的 默認構造函數
    date d2(2010, 1, 1);
    date d3(2000, Jan, 1);            //使用英文指定月份
    date d4(d2);

    assert(d1 == date(boost::date_time::special_values::not_a_date_time));
    assert(d2 == d4);                //date支持比較操做
    assert(d3 < d4);
//從字符串建立
    //注意:包含源碼是依賴 #include <libs/date_time/src/gregorian/greg_month.cpp>
    date d1_1 = boost::gregorian::from_string("1999-12-31");
    date d2_1(boost::gregorian::from_string("2005/1/1"));
    date d3_1 = boost::gregorian::from_undelimited_string("20011118");
//使用 day_clock 建立,返回當天日期對象,注意:依賴操做系統的時區設置
    cout << boost::gregorian::day_clock::local_day() << endl;
    cout << day_clock::local_day_ymd << endl;
    cout << day_clock::universal_day() << endl;
    cout << day_clock::universal_day_ymd << endl;

//建立特殊日期
    date d1_2(special_values::neg_infin);        //負無限日期
    date d2_2(special_values::pos_infin);        //正無限日期
    date d3_2(special_values::not_a_date_time);    //無效日期
    date d4_2(special_values::max_date_time);    //最大可能日期9999-12-31
    date d5_2(special_values::min_date_time);    //最小可能日期1400-01-01

//異常建立,boost會拋出異常。
    date d1_3(1399, 12, 1);
    date d2_3(10000, 1, 1);
    date d3_3(2010, 2, 29);
相關文章
相關標籤/搜索