boost 時間與日期

要點

一、timer庫 P33

  1. timer

    
    #include <boost/timer.hpp>  
    
    using namespace boost;  
  2. progress_timer P36

    繼承自timer,析構時自動輸出時間

    
    #include <boost/progress.hpp>
    
    using namespace boost;
    //將progress_timer的輸出轉移到時stringstream,轉換爲其他字符串
    stringstream ss; //一個字符串流對象
    {
         progress_timer t(ss); //要求progress_timer輸出到ss中
    }
    cout<<ss.str();
  3. progress_display P39

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

二、date_time庫 P43

  1. 日期 date P45

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

    a. from_string() 、 from_undelimited_string() 分隔符分隔年月日格式的字符串、無分隔符的純字符串
    b. day_clock::local_day()、day_clock::universal_day() 本地時間和UTC時間
    c. 日期輸出 P49:

    • 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格式的數字字符串
      d. 日期長度 date_duration P50
      e. 日期區間 date_period P52
      date_period dp( date( 2017, 1, 1 ), days(20) );
      assert( !dp.is_null() );
      assert( dp.begin().day() == 1 );
      assert( dp.last().day() == 20 );
      assert( dp.end().day() == 21 );
      assert( dp.length().days() == 20 );

    f. 日期迭代器 P55

    date_iterator 、week_iterator、month_iterator、year_iterator

  2. 處理時間 P59

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

    a. 時間長度 time_duration P60

    • 一般精度爲微秒級,在 ^1 之前定義宏BOOST_DATE_POSIX_TIME_STD_CONFIG,可以精確到納秒級。
    • 子類 hours、minutes、seconds、millisec/milliseconds、microsec/microseconds、nanoseconds
    • 工廠函數 duration_from_string() : time_duration td = duration_from_string(“1:10:30:001”) //時、分、秒、微秒
    • to_simple_string(time_duration) 和 to_iso_string(time_duration): HH:MM:SS.FFFFFFFF ; HHMMSS,FFFFFFFF
      b. 時間點 ptime類 P64
      c. 例子 高精度計時器 ptimer P68
      d. 例子 計算工作時間 P69
      e. 格式化時間 P71
       date d(2010,3,6);
      date_facet* dfacet = new date_facet("%Y%m%d日");
      cout.imbue(locale(cout.getloc(),dfacet));
      cout << d << endl;
      time_facet *tfacet = new time_facet("%Y%m%d%H%M%S%F秒");
      cout.imbue(locale(cout.getloc(), tfacet));
      cout << ptime(d, hours(21) + minutes(50) + millisec(100)) << endl;

    運行結果如下:
    2010年03月06日
    2010年03月06日21點50分00.100000秒
    f. 本地時間 P72

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

    g. 序列化 P74

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章