最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++  boost 時(shí)間與日期處理詳細(xì)介紹

 更新時(shí)間:2016年11月14日 16:11:10   作者:松陽(yáng)  
這篇文章主要介紹了C++ boost 時(shí)間與日期處理詳細(xì)介紹的相關(guān)資料,這里提供實(shí)例代碼,及實(shí)現(xiàn)效果,需要的朋友可以參考下

boost 時(shí)間與日期處理

導(dǎo)視:

特點(diǎn)
缺點(diǎn)
說(shuō)明
timer
計(jì)時(shí)基類
不適合大跨度時(shí)間
適用大部分的普通計(jì)時(shí)
progress_timer
繼承自timer 可以自動(dòng)寫入流中
只精確到0.01s
如果需要更精確,可派生個(gè)類,調(diào)用stream的precision設(shè)置
progress_display 圖形化顯示進(jìn)度 只能輸出到cout 如果還有其他輸出則會(huì)干擾進(jìn)度顯示。
折中的辦法是重新顯示 pd.restart(size); pd+= pNum;
date 日期結(jié)構(gòu),時(shí)間點(diǎn) —— date是date_time庫(kù)的核心類 boost::gregorian
date_duration days、months、years 時(shí)間段 —— 表示一段時(shí)間,可以把它看成一個(gè)int
date_period 標(biāo)量,左開右閉,時(shí)間區(qū)間 —— 可以認(rèn)為是一個(gè)有起點(diǎn)的date_duration。能做交集、并集
date_iterator 迭代器,以某個(gè)單位增減 —— 天、周、月、年四種迭代器,以某種增量移動(dòng)。
time_duration 時(shí)間段 同date_duration —— hours、minutes、seconds、millisec、boost::posix_time
ptime 時(shí)間點(diǎn) date+time_duration —— 分date()和time_of_day()操作。
time_period 時(shí)間區(qū)間 同date_period —— ——
time_iterator 迭代器,以某個(gè)單位增減 —— 可直接與ptime比較
date_facet 流格式化日期 —— %Y年%m月%d日
time_facet 流格式化時(shí)間 —— %Y年%m月%d日 %H點(diǎn)%M分%S%F秒
#include <boost/timer.hpp> 
#include <boost/progress.hpp> 
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <Windows.h> 
 
#include <boost/date_time/gregorian/gregorian.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 
 
using namespace std; 
 
int main() 
{ 
  boost::timer t; 
  std::cout<<"Max "<<t.elapsed_max()<<endl; 
  std::cout<<"Min "<<t.elapsed_min()<<endl; 
  std::cout<<"elapsed: "<<t.elapsed()<<endl; 
  t.restart(); 
  Sleep(100); 
  std::cout<<"elapsed: "<<t.elapsed()<<endl;  
  cout<<"---------------------------"<<endl; 
  stringstream ss; 
  { 
    boost::progress_timer t(ss);  
    Sleep(300); 
  } 
  cout<<ss.str(); 
  cout<<"---------------------------"<<endl; 
 
  vector<string> v(100); 
  //Do Data Fill......  
  ofstream fs("c:\test.txt"); 
 
  boost::progress_display pd(v.size()); 
  vector<string>::iterator pos; 
  for (pos = v.begin();pos != v.end();++pos) 
  { 
    fs<<*pos<<endl; 
    Sleep(10); 
    ++pd; 
    //pd.restart(v.size()); 
    //pd+=(pos-v.begin() +1); 
  } 
  cout<<"---------------------------"<<endl; 
   
  { 
    using namespace boost::gregorian; 
    cout<<"----------------- date ------------------"<<endl; 
    date d1; 
    date d2(2013,4,7); 
    date d3(2013,Apr,7); 
    date d4(d2); 
 
    assert(d1 == date(not_a_date_time)); //默認(rèn)初始化為無(wú)效日期 
    assert(d2 == d4); 
    assert(d3 == d2); 
     
    d1 = from_string("1999,9,9"); 
    date d5 (from_string("2008/8/8")); 
    d3 = from_undelimited_string("20110111"); 
     
    cout<<day_clock::local_day()<<endl; 
    cout<<day_clock::universal_day()<<endl; 
 
    date d6 (neg_infin); 
    date d7(pos_infin); 
    cout<<d6<<endl; 
    cout<<d7<<endl; 
 
    cout<<"---------------------------"<<endl; 
    date today (2013,4,17); 
    assert(today.year() == 2013); 
    assert(today.month() == 4); 
    assert(today.day() == 17); 
 
    date::ymd_type ymd = today.year_month_day(); 
    assert(ymd.year == 2013); 
    assert(ymd.month == 4); 
    assert(ymd.day == 17); 
 
    assert(today.day_of_week() == 3); //星期幾 周日為0 
    cout<<today.day_of_year()<<endl;  //在一年中是第幾天 
    assert(today.end_of_month() == date(2013,4,30));  //當(dāng)月的最后一天 
    cout<<today.week_number()<<endl;  //當(dāng)年的第幾周 范圍0~53 年初的半周歸為上一年,即53 
    assert(d6.is_infinity());      //日期為無(wú)限日期 
    assert(d6.is_neg_infinity()); 
    cout<<"---------------------------"<<endl; 
 
    cout<<to_simple_string(today)<<endl; 
    cout<<to_iso_string(today)<<endl;     
    cout<<to_iso_extended_string(today)<<endl; //常用日期格式Y(jié)YYY-MM-DD 
    cout<<today<<endl; 
   
    cout<<"---------------------------"<<endl; 
    tm t = to_tm(today);   
    assert(t.tm_hour == 0 && t.tm_min == 0); 
     
    date new_today = date_from_tm(t);  //從tm轉(zhuǎn)為date 
    assert(new_today == today); 
 
    cout<<"-------------- days(date_duration) --------------"<<endl; 
    days dd1(10),dd2(-20),dd3(365); 
    assert(dd1>dd2 &&dd1<dd3); 
    assert(dd1+dd2 == days(-10)); 
    assert((dd2+dd3).days() == 345); 
    assert(dd3/5 == days(73)); 
 
    weeks w(3);   //3個(gè)星期 
    assert(w.days() == 21); 
     
    months m(5); 
    years y(2); 
 
    months m2 = y+m; 
    assert(m2.number_of_months() == 29); 
    assert((y*2).number_of_years() == 4); 
 
    cout<<"-------------- Calc --------------"<<endl; 
    date dA(2000,1,1),dB(2008,8,8); 
    cout<<dB-dA<<endl;   //3142天 
     
    dA+=days(10); 
    assert(dA.day() == 11); 
    dA+=months(2); 
    assert(dA.month() ==3 && dA.day()== 11); 
 
    dA-=weeks(1); 
    assert(dA.day() == 4); 
 
    dB-=years(7); 
    assert(dA.year() == dB.year()-1); 
 
    //如果日期是月末的最后一天,加減月或年會(huì)得到月末的時(shí)間,而不是簡(jiǎn)單的月、年加1 
    date sp(2013,3,30); 
    sp-=months(1); 
    assert(sp.month() == 2 && sp.day() == 28); 
    sp -=months(1); 
    assert(sp.month()== 1 && sp.day()== 31); 
    sp+=months(2); 
    assert(sp.day() == 31); //與原來(lái)的日期已經(jīng)不相等! 
 
    cout<<"-------------- date_period --------------"<<endl; 
    date_period dp(date(2013,4,17),days(14));  //左開右閉與STL的容器相似 
    assert(!dp.is_null()); 
    assert(dp.begin().day() == 17); 
    assert(dp.last().day() == 30); 
    assert(dp.end().day() == 1); 
 
    cout<<dp<<endl; 
 
    date_period new_dp = dp; 
    new_dp.shift(days(3));   //將時(shí)間區(qū)間向后移動(dòng) 
    assert(new_dp.begin().day() == 20); 
    assert(new_dp.length().days() == 14); 
 
     
    new_dp.expand(days(3));   //區(qū)間兩段延長(zhǎng)n天,即延長(zhǎng)2n天。 
    assert(new_dp.begin().day() == 17); 
    assert(new_dp.length().days() == 20); 
 
    assert(dp.is_after(date(2013,1,1))); 
    assert(dp.contains(date(2013,4,20))); 
 
    date_period dp2 (date(2013,4,17),days(5)); 
    assert(dp.contains(dp2)); 
     
    assert(dp.intersects(dp2));   //交集 
    assert(dp.intersection(dp2) == dp2); 
     
    date_period dp3 (date(2013,5,1),days(5)); 
    assert(!dp3.intersects(dp)); 
    assert(dp3.intersection(dp2).is_null()); 
 
    assert(dp.is_adjacent(dp3)); 
     
    date_period dp4(date(2013,4,17),days(19)); //并集 
    assert(dp.merge(dp3).is_null());  //無(wú)交集返回空 
    assert(dp.span(dp3) == dp4);    //填充中間區(qū)域 
 
 
    cout<<"-------------- date_iterator --------------"<<endl; 
    date last(2013,4,17); 
 
    day_iterator d_iter(last); //日期迭代器 
 
    assert(d_iter == last); 
    ++d_iter; 
    assert(d_iter == date(2013,4,18)); 
 
    year_iterator y_iter(*d_iter,3);  //增減步長(zhǎng)為3 
    assert(y_iter == last + days(1)); 
 
    ++y_iter; 
    assert(y_iter->year() == 2016); 
   
    cout<<"-------------- func --------------"<<endl; 
    cout<<(gregorian_calendar::is_leap_year(2000)? "Yes":"no")<<endl;  //閏年 
    assert(gregorian_calendar::end_of_month_day(2013,2) == 28);   //月末天 
     
  } 
   
  { 
    using namespace boost::posix_time; 
    cout<<"-------------- time_duration --------------"<<endl; 
    time_duration td(1,1,1);  //時(shí)、分、秒 會(huì)自動(dòng)借、進(jìn)位 
    hours h0(1); 
    minutes m(1); 
    seconds s(1); 
    millisec ms(1); 
     
    time_duration td2 = h0+m+s+ms; 
    time_duration td3 = hours(2) + minutes(10); 
    time_duration td4 = duration_from_string("1:10:10:300"); 
 
    assert(td4.hours() == 1 && td4.minutes() == 10 && td4.seconds() == 10);  
    assert(td.total_seconds() == 1*3600 + 1*60 +1); //轉(zhuǎn)為sec 
 
    hours h(-10); 
    assert(h.is_negative()); 
 
    time_duration h2 = h.invert_sign(); //取反 
    assert(!h2.is_negative() && h2.hours() == 10); 
 
    cout<<td3-td2<<endl; 
    cout<<to_simple_string(td4)<<endl; 
    cout<<to_iso_string(td4)<<endl; 
     
    cout<<"-------------- ptime --------------"<<endl; 
    { 
      using namespace boost::gregorian; 
      ptime p(date(2013,4,17),hours(1)); //ptime相當(dāng)于date+time_duration 
      ptime p1 = time_from_string("2013-4-17 16:25:00"); 
      cout<<p<<endl; 
      cout<<p1<<endl; 
      ptime p2 = second_clock::local_time();     //常用時(shí)間輸出 
      ptime p3 = microsec_clock::universal_time();  //微秒精度 
      cout<<p2<<endl<<p3<<endl; 
 
      ptime op(date(2013,4,17),hours(1)+minutes(30)); 
 
      date d = op.date(); 
      time_duration optd = op.time_of_day(); 
      assert(d.day() == 17 && d.month() == 4); 
      assert(optd.hours() == 1 && optd.minutes() == 30); 
      cout<<to_iso_extended_string(op)<<endl; 
 
      tm t = to_tm(op);  //不可逆,此處與date不同 
                //只能用date_from_tm先得到日期,再填充時(shí)間。 
       
      cout<<"-------------- time_period --------------"<<endl; 
      time_period tp1 (op,hours(8)); 
      time_period tp2(op+hours(8),hours(1)); 
      assert(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2)); 
      assert(!tp1.intersects(tp2)); 
 
      tp1.shift(hours(1)); 
      assert(tp1.is_after(op)); 
      assert(tp1.intersects(tp2)); 
 
      tp2.expand(hours(10)); 
      assert(tp2.contains(op) && tp2.contains(tp1)); 
 
      cout<<"-------------- time_iterator --------------"<<endl; 
      for (time_iterator t_iter(op,minutes(10));t_iter<op+hours(1);++t_iter) 
      { 
        cout<<*t_iter<<endl; 
      } 
      cout<<"-------------- formate --------------"<<endl; 
      date_facet* dfacet = new date_facet("%Y 年%m 月%d 日"); 
      cout.imbue(locale(cout.getloc(),dfacet)); 
      cout<<date(2013,4,17)<<endl; 
 
      time_facet* tfacet = new time_facet("%Y 年%m 月%d 日 %H點(diǎn)%M分%S%F秒"); 
      cout.imbue(locale(cout.getloc(),tfacet)); 
      cout<<op<<endl; 
    } 
  } 
 
  getchar(); 
  return 0; 
} 
運(yùn)行結(jié)果:
Max 2.14748e+006 
Min 0.001 
elapsed: 0.001 
elapsed: 0.1 
--------------------------- 
0.30 s 
--------------------------- 
0% 10 20 30 40 50 60 70 80 90 100% 
|----|----|----|----|----|----|----|----|----|----| 
*************************************************** 
--------------------------- 
----------------- date ------------------ 
2013-Apr-17 
2013-Apr-17 
-infinity 
+infinity 
--------------------------- 
107 
16 
--------------------------- 
2013-Apr-17 
20130417 
2013-04-17 
2013-Apr-17 
--------------------------- 
-------------- days(date_duration) -------------- 
-------------- Calc -------------- 
3142 
-------------- date_period -------------- 
[2013-Apr-17/2013-Apr-30] 
-------------- date_iterator -------------- 
-------------- func -------------- 
Yes 
-------------- time_duration -------------- 
01:08:58.999000 
01:10:10.300000 
011010.300000 
-------------- ptime -------------- 
2013-Apr-17 01:00:00 
2013-Apr-17 16:25:00 
2013-Apr-17 17:19:21 
2013-Apr-17 09:19:21.870604 
2013-04-17T01:30:00 
-------------- time_period -------------- 
-------------- time_iterator -------------- 
2013-Apr-17 01:30:00 
2013-Apr-17 01:40:00 
2013-Apr-17 01:50:00 
2013-Apr-17 02:00:00 
2013-Apr-17 02:10:00 
2013-Apr-17 02:20:00 
-------------- formate -------------- 
2013 年04 月17 日 
2013 年04 月17 日 01點(diǎn)30分00秒 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 詳解C++17中if和switch語(yǔ)句的新特性

    詳解C++17中if和switch語(yǔ)句的新特性

    這篇文章主要為大家詳細(xì)介紹了C++17中if和switch語(yǔ)句的新特性的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • C++類與對(duì)象之日期類的實(shí)現(xiàn)

    C++類與對(duì)象之日期類的實(shí)現(xiàn)

    這篇文章主要介紹如何實(shí)現(xiàn)C++中的日期類相關(guān)資料,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • C/C++中指針的深入理解

    C/C++中指針的深入理解

    指針在 C\C++ 語(yǔ)言中是很重要的內(nèi)容,并且和指針有關(guān)的內(nèi)容一向令初學(xué)者頭大,這篇文章主要給大家介紹了關(guān)于C/C++中指針的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • C++實(shí)現(xiàn)猜數(shù)小游戲的實(shí)現(xiàn)

    C++實(shí)現(xiàn)猜數(shù)小游戲的實(shí)現(xiàn)

    這篇文章主要介紹了C++實(shí)現(xiàn)猜數(shù)小游戲的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • C++實(shí)現(xiàn)list增刪查改模擬的示例代碼

    C++實(shí)現(xiàn)list增刪查改模擬的示例代碼

    本文主要介紹了C++實(shí)現(xiàn)list增刪查改模擬,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • C++利用Opencv實(shí)現(xiàn)多個(gè)圓形檢測(cè)

    C++利用Opencv實(shí)現(xiàn)多個(gè)圓形檢測(cè)

    霍夫圓檢測(cè)是opencv中用來(lái)檢測(cè)圓的重要算法,簡(jiǎn)單的說(shuō),霍夫圓檢測(cè)就是對(duì)圖像中的弧線做切線,再在切點(diǎn)位置做切線的垂線,然后看這些垂線能交于一點(diǎn)的個(gè)數(shù),這個(gè)在方法中是自己設(shè)定的
    2022-08-08
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易五子棋

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易五子棋

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • C++實(shí)現(xiàn)教職工信息管理系統(tǒng)課程設(shè)計(jì)

    C++實(shí)現(xiàn)教職工信息管理系統(tǒng)課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)教職工信息管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C語(yǔ)言實(shí)現(xiàn)掃雷小游戲詳解

    C語(yǔ)言實(shí)現(xiàn)掃雷小游戲詳解

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 基于C++實(shí)現(xiàn)日期計(jì)算器的詳細(xì)教程

    基于C++實(shí)現(xiàn)日期計(jì)算器的詳細(xì)教程

    在現(xiàn)代社會(huì)中,計(jì)算器已經(jīng)進(jìn)入了每一個(gè)家庭,人們?cè)谏詈蛯W(xué)習(xí)中經(jīng)常需要使用到計(jì)算器,下面這篇文章主要給大家介紹了關(guān)于基于C++實(shí)現(xiàn)日期計(jì)算器的相關(guān)資料,需要的朋友可以參考下
    2022-06-06

最新評(píng)論

四平市| 商河县| 上高县| 威海市| 临高县| 闽侯县| 贡嘎县| 浦北县| 廊坊市| 仲巴县| 丰县| 历史| 静海县| 漠河县| 永靖县| 哈巴河县| 辰溪县| 九江市| 宕昌县| 津市市| 阿巴嘎旗| 新民市| 乐陵市| 丽江市| 巨野县| 永康市| 钦州市| 东源县| 左云县| 明光市| 德江县| 大冶市| 根河市| 青海省| 和田市| 巴中市| 荔浦县| 繁昌县| 永康市| 仲巴县| 大足县|