boost 时间与日期处理

来源:互联网 发布:身边低调的有钱人知乎 编辑:程序博客网 时间:2024/05/01 22:13

【Boost】time_duration, time_period, time_iterator

time_duration的例子

[cpp] view plain copy
 print?
  1. void test_ptime_duration()  
  2. {  
  3.     using namespace boost::posix_time;  
  4.     using namespace boost::gregorian;  
  5.   
  6.     // time_duration构造的常用方法  
  7.     time_duration td1(1, 2, 3, 4);  
  8.     time_duration td2 = time_duration(1, 2, 3) + milliseconds(4) + microseconds(5);  
  9.     time_duration td3 = hours(1) + minutes(2) + seconds(3) + milliseconds(4) + microseconds(5);  
  10.     time_duration td4(duration_from_string("01:02:03.000"));  
  11.   
  12.     // 计算总共的秒数, 毫秒数, 微秒数.  
  13.     std::cout << td1.total_seconds() << std::endl;  
  14.     std::cout << td1.total_milliseconds() << std::endl;  
  15.     std::cout << td1.total_microseconds() << std::endl;  
  16. }  
time_period的例子
[cpp] view plain copy
 print?
  1. void test_ptime_period()  
  2. {  
  3.     using namespace boost::posix_time;  
  4.     using namespace boost::gregorian;  
  5.   
  6.     // time_period构造的常用方法, 注意这样的构造函数是左闭右开, 如果end <= begin則时间段定义为无效.  
  7.     time_period tp1(ptime(date(2010, 11, 29), hours(11) + minutes(23) + seconds(45)),   
  8.                     ptime(date(2010, 12, 10), hours(22) + minutes(45) + seconds(56) + milliseconds(10)));     
  9.     time_period tp2(ptime(date(2010, 11, 29), hours(11) + minutes(23) + seconds(45)),  
  10.                     time_duration(2, 3, 4, 5));   
  11.     time_period tp3(tp2);  
  12.   
  13.     std::cout << tp1 << std::endl;  
  14.     std::cout << tp2 << std::endl;  
  15.   
  16.     // shift, expand  
  17.     time_period tp5(tp2);  
  18.     tp5.shift(time_duration(1, 2, 3, 4));   
  19.     time_period tp6(tp2);  
  20.     tp6.expand(time_duration(2, 3, 4, 5));   
  21.     std::cout << tp5 << std::endl;  
  22.     std::cout << tp6 << std::endl;  
  23.   
  24.     // begin, last, end  
  25.     ptime pt1 = tp2.begin();  
  26.     ptime pt2 = tp2.end();  
  27.     ptime pt3 = tp2.last();  
  28.     std::cout << pt1 << std::endl;  
  29.     std::cout << pt2 << std::endl;  
  30.     std::cout << pt3 << std::endl;  
  31.   
  32.     // length, merge, span, intersects, intersection  
  33.     // 同date, 参见: http://blog.csdn.net/huang_xw/article/details/8239518   
  34. }  
time_iterator的例子
[cpp] view plain copy
 print?
  1. void test_ptime_iterator()  
  2. {  
  3.     using namespace boost::gregorian;  
  4.     using namespace boost::posix_time;  
  5.     date d(2012, 11, 30);  
  6.     ptime start(d);  
  7.     ptime end = start + hours(1);  
  8.     // 每次递增15分钟  
  9.     time_iterator titr(start, minutes(15));   
  10.     // 生成 00:00:00, 00:15:00, 00:30:00, 00:45:00  
  11.     while (titr < end) {  
  12.         std::cout << to_simple_string(*titr) << std::endl;  
  13.         ++titr;  
  14.     }  
  15. }  
 38人阅读 评论(0) 收藏 举报
特点缺点说明timer计时基类不适合大跨度时间适用大部分的普通计时progress_timer继承自timer 可以自动写入流中只精确到0.01s如果需要更精确,可派生个类,调用stream的precision设置progress_display图形化显示进度只能输出到cout如果还有其他输出则会干扰进度显示。
折中的办法是重新显示 pd.restart(size); pd+= pNum;
date日期结构,时间点——date是date_time库的核心类 boost::gregoriandate_durationdays、months、years 时间段——表示一段时间,可以把它看成一个intdate_period标量,左开右闭,时间区间——可以认为是一个有起点的date_duration。能做交集、并集date_iterator迭代器,以某个单位增减——天、周、月、年四种迭代器,以某种增量移动。time_duration时间段 同date_duration——hours、minutes、seconds、millisec、boost::posix_timeptime时间点 date+time_duration——分date()和time_of_day()操作。time_period时间区间 同date_period————time_iterator迭代器,以某个单位增减——可直接与ptime比较date_facet流格式化日期——%Y年%m月%d日time_facet流格式化时间——%Y年%m月%d日 %H点%M分%S%F秒
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <boost/timer.hpp>  
  2. #include <boost/progress.hpp>  
  3. #include <iostream>  
  4. #include <sstream>  
  5. #include <fstream>  
  6. #include <string>  
  7. #include <vector>  
  8. #include <Windows.h>  
  9.   
  10. #include <boost/date_time/gregorian/gregorian.hpp>  
  11. #include <boost/date_time/posix_time/posix_time.hpp>  
  12.   
  13. using namespace std;  
  14.   
  15. int main()  
  16. {  
  17.     boost::timer t;  
  18.     std::cout<<"Max "<<t.elapsed_max()<<endl;  
  19.     std::cout<<"Min "<<t.elapsed_min()<<endl;  
  20.     std::cout<<"elapsed: "<<t.elapsed()<<endl;  
  21.     t.restart();  
  22.     Sleep(100);  
  23.     std::cout<<"elapsed: "<<t.elapsed()<<endl;   
  24.     cout<<"---------------------------"<<endl;  
  25.     stringstream ss;  
  26.     {  
  27.         boost::progress_timer t(ss);   
  28.         Sleep(300);  
  29.     }  
  30.     cout<<ss.str();  
  31.     cout<<"---------------------------"<<endl;  
  32.   
  33.     vector<string> v(100);  
  34.     //Do Data Fill......   
  35.     ofstream fs("c:\test.txt");  
  36.   
  37.     boost::progress_display pd(v.size());  
  38.     vector<string>::iterator pos;  
  39.     for (pos = v.begin();pos != v.end();++pos)  
  40.     {  
  41.         fs<<*pos<<endl;  
  42.         Sleep(10);  
  43.         ++pd;  
  44.         //pd.restart(v.size());  
  45.         //pd+=(pos-v.begin() +1);  
  46.     }  
  47.     cout<<"---------------------------"<<endl;  
  48.       
  49.     {  
  50.         using namespace boost::gregorian;  
  51.         cout<<"-----------------  date ------------------"<<endl;  
  52.         date d1;  
  53.         date d2(2013,4,7);  
  54.         date d3(2013,Apr,7);  
  55.         date d4(d2);  
  56.   
  57.         assert(d1 == date(not_a_date_time)); //默认初始化为无效日期  
  58.         assert(d2 == d4);  
  59.         assert(d3 == d2);  
  60.           
  61.         d1 = from_string("1999,9,9");  
  62.         date d5 (from_string("2008/8/8"));  
  63.         d3 = from_undelimited_string("20110111");  
  64.           
  65.         cout<<day_clock::local_day()<<endl;  
  66.         cout<<day_clock::universal_day()<<endl;  
  67.   
  68.         date d6 (neg_infin);  
  69.         date d7(pos_infin);  
  70.         cout<<d6<<endl;  
  71.         cout<<d7<<endl;  
  72.   
  73.         cout<<"---------------------------"<<endl;  
  74.         date today (2013,4,17);  
  75.         assert(today.year() == 2013);  
  76.         assert(today.month() == 4);  
  77.         assert(today.day() == 17);  
  78.   
  79.         date::ymd_type ymd = today.year_month_day();  
  80.         assert(ymd.year == 2013);  
  81.         assert(ymd.month == 4);  
  82.         assert(ymd.day == 17);  
  83.   
  84.         assert(today.day_of_week() == 3); //星期几 周日为0  
  85.         cout<<today.day_of_year()<<endl;    //在一年中是第几天  
  86.         assert(today.end_of_month() == date(2013,4,30));    //当月的最后一天  
  87.         cout<<today.week_number()<<endl;    //当年的第几周 范围0~53 年初的半周归为上一年,即53  
  88.         assert(d6.is_infinity());           //日期为无限日期  
  89.         assert(d6.is_neg_infinity());  
  90.         cout<<"---------------------------"<<endl;  
  91.   
  92.         cout<<to_simple_string(today)<<endl;  
  93.         cout<<to_iso_string(today)<<endl;         
  94.         cout<<to_iso_extended_string(today)<<endl;  //常用日期格式YYYY-MM-DD  
  95.         cout<<today<<endl;  
  96.       
  97.         cout<<"---------------------------"<<endl;  
  98.         tm t = to_tm(today);      
  99.         assert(t.tm_hour == 0 && t.tm_min == 0);  
  100.           
  101.         date new_today = date_from_tm(t);   //从tm转为date  
  102.         assert(new_today == today);  
  103.   
  104.         cout<<"-------------- days(date_duration) --------------"<<endl;  
  105.         days dd1(10),dd2(-20),dd3(365);  
  106.         assert(dd1>dd2 &&dd1<dd3);  
  107.         assert(dd1+dd2 == days(-10));  
  108.         assert((dd2+dd3).days() == 345);  
  109.         assert(dd3/5 == days(73));  
  110.   
  111.         weeks w(3);     //3个星期  
  112.         assert(w.days() == 21);  
  113.           
  114.         months m(5);  
  115.         years y(2);  
  116.   
  117.         months m2 = y+m;  
  118.         assert(m2.number_of_months() == 29);  
  119.         assert((y*2).number_of_years() == 4);  
  120.   
  121.         cout<<"-------------- Calc --------------"<<endl;  
  122.         date dA(2000,1,1),dB(2008,8,8);  
  123.         cout<<dB-dA<<endl;      //3142天  
  124.           
  125.         dA+=days(10);  
  126.         assert(dA.day() == 11);  
  127.         dA+=months(2);  
  128.         assert(dA.month() ==3 && dA.day()== 11);  
  129.   
  130.         dA-=weeks(1);  
  131.         assert(dA.day() == 4);  
  132.   
  133.         dB-=years(7);  
  134.         assert(dA.year() == dB.year()-1);  
  135.   
  136.         //如果日期是月末的最后一天,加减月或年会得到月末的时间,而不是简单的月、年加1  
  137.         date sp(2013,3,30);  
  138.         sp-=months(1);  
  139.         assert(sp.month() == 2 && sp.day() == 28);  
  140.         sp -=months(1);  
  141.         assert(sp.month()== 1 && sp.day()== 31);  
  142.         sp+=months(2);  
  143.         assert(sp.day() == 31); //与原来的日期已经不相等!  
  144.   
  145.         cout<<"-------------- date_period --------------"<<endl;  
  146.         date_period dp(date(2013,4,17),days(14));   //左开右闭与STL的容器相似  
  147.         assert(!dp.is_null());  
  148.         assert(dp.begin().day() == 17);  
  149.         assert(dp.last().day() == 30);  
  150.         assert(dp.end().day() == 1);  
  151.   
  152.         cout<<dp<<endl;  
  153.   
  154.         date_period new_dp = dp;  
  155.         new_dp.shift(days(3));      //将时间区间向后移动  
  156.         assert(new_dp.begin().day() == 20);  
  157.         assert(new_dp.length().days() == 14);  
  158.   
  159.           
  160.         new_dp.expand(days(3));     //区间两段延长n天,即延长2n天。  
  161.         assert(new_dp.begin().day() == 17);  
  162.         assert(new_dp.length().days() == 20);  
  163.   
  164.         assert(dp.is_after(date(2013,1,1)));  
  165.         assert(dp.contains(date(2013,4,20)));  
  166.   
  167.         date_period dp2 (date(2013,4,17),days(5));  
  168.         assert(dp.contains(dp2));  
  169.           
  170.         assert(dp.intersects(dp2));     //交集  
  171.         assert(dp.intersection(dp2) == dp2);  
  172.           
  173.         date_period dp3 (date(2013,5,1),days(5));  
  174.         assert(!dp3.intersects(dp));  
  175.         assert(dp3.intersection(dp2).is_null());  
  176.   
  177.         assert(dp.is_adjacent(dp3));  
  178.           
  179.         date_period dp4(date(2013,4,17),days(19)); //并集  
  180.         assert(dp.merge(dp3).is_null());    //无交集返回空  
  181.         assert(dp.span(dp3) == dp4);        //填充中间区域  
  182.   
  183.   
  184.         cout<<"-------------- date_iterator --------------"<<endl;  
  185.         date last(2013,4,17);  
  186.   
  187.         day_iterator d_iter(last);  //日期迭代器  
  188.   
  189.         assert(d_iter == last);  
  190.         ++d_iter;  
  191.         assert(d_iter == date(2013,4,18));  
  192.   
  193.         year_iterator y_iter(*d_iter,3);    //增减步长为3  
  194.         assert(y_iter == last + days(1));  
  195.   
  196.         ++y_iter;  
  197.         assert(y_iter->year() == 2016);  
  198.       
  199.         cout<<"-------------- func --------------"<<endl;  
  200.         cout<<(gregorian_calendar::is_leap_year(2000)? "Yes":"no")<<endl;   //闰年  
  201.         assert(gregorian_calendar::end_of_month_day(2013,2) == 28);     //月末天  
  202.           
  203.     }  
  204.       
  205.     {  
  206.         using namespace boost::posix_time;  
  207.         cout<<"-------------- time_duration --------------"<<endl;  
  208.         time_duration td(1,1,1);    //时、分、秒 会自动借、进位  
  209.         hours h0(1);  
  210.         minutes m(1);  
  211.         seconds s(1);  
  212.         millisec ms(1);  
  213.           
  214.         time_duration td2 = h0+m+s+ms;  
  215.         time_duration td3 = hours(2) + minutes(10);  
  216.         time_duration td4 = duration_from_string("1:10:10:300");  
  217.   
  218.         assert(td4.hours() == 1 && td4.minutes() == 10 && td4.seconds() == 10);   
  219.         assert(td.total_seconds() == 1*3600 + 1*60 +1); //转为sec  
  220.   
  221.         hours h(-10);  
  222.         assert(h.is_negative());  
  223.   
  224.         time_duration h2 = h.invert_sign(); //取反  
  225.         assert(!h2.is_negative() && h2.hours() == 10);  
  226.   
  227.         cout<<td3-td2<<endl;  
  228.         cout<<to_simple_string(td4)<<endl;  
  229.         cout<<to_iso_string(td4)<<endl;  
  230.           
  231.         cout<<"-------------- ptime --------------"<<endl;  
  232.         {  
  233.             using namespace boost::gregorian;  
  234.             ptime p(date(2013,4,17),hours(1));  //ptime相当于date+time_duration  
  235.             ptime p1 = time_from_string("2013-4-17 16:25:00");  
  236.             cout<<p<<endl;  
  237.             cout<<p1<<endl;  
  238.             ptime p2 = second_clock::local_time();          //常用时间输出  
  239.             ptime p3 = microsec_clock::universal_time();    //微秒精度  
  240.             cout<<p2<<endl<<p3<<endl;  
  241.   
  242.             ptime op(date(2013,4,17),hours(1)+minutes(30));  
  243.   
  244.             date d = op.date();  
  245.             time_duration optd = op.time_of_day();  
  246.             assert(d.day() == 17 && d.month() == 4);  
  247.             assert(optd.hours() == 1 && optd.minutes() == 30);  
  248.             cout<<to_iso_extended_string(op)<<endl;  
  249.   
  250.             tm t = to_tm(op);   //不可逆,此处与date不同  
  251.                                 //只能用date_from_tm先得到日期,再填充时间。  
  252.               
  253.             cout<<"-------------- time_period --------------"<<endl;  
  254.             time_period tp1 (op,hours(8));  
  255.             time_period tp2(op+hours(8),hours(1));  
  256.             assert(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2));  
  257.             assert(!tp1.intersects(tp2));  
  258.   
  259.             tp1.shift(hours(1));  
  260.             assert(tp1.is_after(op));  
  261.             assert(tp1.intersects(tp2));  
  262.   
  263.             tp2.expand(hours(10));  
  264.             assert(tp2.contains(op) && tp2.contains(tp1));  
  265.   
  266.             cout<<"-------------- time_iterator --------------"<<endl;  
  267.             for (time_iterator t_iter(op,minutes(10));t_iter<op+hours(1);++t_iter)  
  268.             {  
  269.                 cout<<*t_iter<<endl;  
  270.             }  
  271.             cout<<"-------------- formate --------------"<<endl;  
  272.             date_facet* dfacet = new date_facet("%Y 年%m 月%d 日");  
  273.             cout.imbue(locale(cout.getloc(),dfacet));  
  274.             cout<<date(2013,4,17)<<endl;  
  275.   
  276.             time_facet* tfacet = new time_facet("%Y 年%m 月%d 日 %H点%M分%S%F秒");  
  277.             cout.imbue(locale(cout.getloc(),tfacet));  
  278.             cout<<op<<endl;  
  279.         }  
  280.     }  
  281.   
  282.     getchar();  
  283.     return 0;  
  284. }  
运行结果:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. Max 2.14748e+006  
  2. Min 0.001  
  3. elapsed: 0.001  
  4. elapsed: 0.1  
  5. ---------------------------  
  6. 0.30 s  
  7. ---------------------------  
  8. 0% 10 20 30 40 50 60 70 80 90 100%  
  9. |----|----|----|----|----|----|----|----|----|----|  
  10. ***************************************************  
  11. ---------------------------  
  12. ----------------- date ------------------  
  13. 2013-Apr-17  
  14. 2013-Apr-17  
  15. -infinity  
  16. +infinity  
  17. ---------------------------  
  18. 107  
  19. 16  
  20. ---------------------------  
  21. 2013-Apr-17  
  22. 20130417  
  23. 2013-04-17  
  24. 2013-Apr-17  
  25. ---------------------------  
  26. -------------- days(date_duration) --------------  
  27. -------------- Calc --------------  
  28. 3142  
  29. -------------- date_period --------------  
  30. [2013-Apr-17/2013-Apr-30]  
  31. -------------- date_iterator --------------  
  32. -------------- func --------------  
  33. Yes  
  34. -------------- time_duration --------------  
  35. 01:08:58.999000  
  36. 01:10:10.300000  
  37. 011010.300000  
  38. -------------- ptime --------------  
  39. 2013-Apr-17 01:00:00  
  40. 2013-Apr-17 16:25:00  
  41. 2013-Apr-17 17:19:21  
  42. 2013-Apr-17 09:19:21.870604  
  43. 2013-04-17T01:30:00  
  44. -------------- time_period --------------  
  45. -------------- time_iterator --------------  
  46. 2013-Apr-17 01:30:00  
  47. 2013-Apr-17 01:40:00  
  48. 2013-Apr-17 01:50:00  
  49. 2013-Apr-17 02:00:00  
  50. 2013-Apr-17 02:10:00  
  51. 2013-Apr-17 02:20:00  
  52. -------------- formate --------------  
  53. 2013 年04 月17 日  
  54. 2013 年04 月17 日 01点30分00秒  
[Boost]boost的时间和日期处理-(1)日期的操作

本篇紧接着boost上篇叙述Boost::DateTime的时间处理。在C++中,常见的时间有time_t, FILETIME和tm,而boost中用ptime。

构造ptime

1.ptime的构造函数有四种:

1:      using namespace boost::posix_time;
2:      using namespace boost::gregorian;
3:  ptime pt(date(2013,Jan,24),time_duration(1,2,3)); //由date和time_duration构造
4:  ptime pt1(date(2013,Jan,24),hours()+nanosec(5));//改变形式的time_duration也能使用
5:  ptime pt2(p1);//拷贝构造函数
6:  ptime pt3(neg_infin);//特殊值构造
7:  ptime p;//默认构造函数,这里p等于not_a_date_time

2.用string构造ptime:

1:      std::string ts1("2013-01-30 23:32:22.000");//固定格式,小数点后支持6位
2:  ptime pt1(time_from_string(ts1));
3:  std::string ts2("20130130T233222");//没有分隔符的date和time
4:  ptime pt2(from_iso_string(ts2));
5:  

3.通过时钟构造ptime:

1:      ptime ct1(second_clock::local_time());
2:  ptime ct2(second_clock::universal_time());
3:  ptime ct3(microsec_clock::local_time());
4:  ptime ct4(microsec_clock::universal_time());
5:  

4.time_t和FILETIME构造ptime:

1:      ptime t = from_time_t(tt); // 其中tt为time_t
2:  ptime t1 = from_ftime<ptime>(ft); //其中ft为FILETIME

ptime访问日期时间

1:      using namespace boost::posix_time;
2:      using namespace boost::gregorian;
3:  ptime now(second_clock::local_time());
4:  std::cout << "today is: " << now.date() << std::endl;
5:  std::cout << "time is: " << now.time_of_day() << std::endl;
6:  

ptime转换为string

1:      std::string now_str(to_simple_string(now));
2:  std::string now_iso_str(to_iso_string(now));
3:  std::string now_iso_ext_str(to_iso_extended_string(now));
4:  std::cout << now_str << std::endl;
5:  std::cout << now_iso_str << std::endl;
6:  std::cout << now_iso_ext_str << std::endl;

ptime与tm,time_t,FILETIME互转

1.tm

 1:     using namespace boost::posix_time;
 2:     using namespace boost::gregorian;
 3:     tm pt_tm;
 4:     pt_tm.tm_year = 113;
 5:     pt_tm.tm_mon = 11;
 6:     pt_tm.tm_mday = 25;
 7:     pt_tm.tm_hour = 2;
 8:     pt_tm.tm_min = 23;
 9:     pt_tm.tm_sec = 40;
10:  
11:     ptime pt = data_from_tm(pt_tm);
12:     std::cout << pt << std::endl;
13:  
14:     pt = pt + hours(2);
15:     tm pt_tm1 = to_tm(pt);

2. time_t

 1:      using namespace boost::posix_time;
 2:      using namespace boost::gregorian;
 3:  
 4:  time_t now = time(NULL);
 5:  std::cout << "time_t : " << now << std::endl;
 6:  ptime now_pt = from_time_t(now);
 7:  std::cout << "ptime from time_t : " << now_pt.time_of_day() << std::endl;
 8:  tm* now_tm = gmtime(&now);
 9:  std::cout << "tm struct: hour : " << now_tm->tm_hour << std::endl;
10:  

3.FILETIME

1:      FILETIME ft;
2:  ft.dwHighDateTime = 29715317;
3:  ft.dwLowDateTime = 3865122988UL
4:      ptime pt = from_ftime<ptime>(ft);
5:  // pt ===> 2005-Jun-07 15:30:57.03958200
6:  

time_duration和time_period

 1:      using namespace boost::posix_time;
 2:      using namespace boost::gregorian;
 3:  
 4:  time_duration td(100,200,3,9);
 5:  std::cout << td << std::endl;
 6:  date d(2013,Feb,5);
 7:  ptime pt(d,minutes(10));
 8:  ptime pt1(d,hours(10));
 9:  time_period tp(pt,pt1);
10:  std::cout << tp << std::endl;
11:  

对于这两者的区别,一个是时间间隔,一个是时间起止的一个窗口。time_duration用于ptime的时间偏移计算为主。而time_period可以计算一个ptime时间点是否在这个时间区间内(参考contains函数)。time_period在创建之后可以扩展,可以平移,函数分别为expand和shift。请大家自己细究。

[Boost]boost的时间和日期处理-(2)时间的操作

本篇紧接着boost上篇叙述Boost::DateTime的时间处理。在C++中,常见的时间有time_t, FILETIME和tm,而boost中用ptime。

构造ptime

1.ptime的构造函数有四种:

1:      using namespace boost::posix_time;
2:      using namespace boost::gregorian;
3:  ptime pt(date(2013,Jan,24),time_duration(1,2,3)); //由date和time_duration构造
4:  ptime pt1(date(2013,Jan,24),hours()+nanosec(5));//改变形式的time_duration也能使用
5:  ptime pt2(p1);//拷贝构造函数
6:  ptime pt3(neg_infin);//特殊值构造
7:  ptime p;//默认构造函数,这里p等于not_a_date_time

2.用string构造ptime:

1:      std::string ts1("2013-01-30 23:32:22.000");//固定格式,小数点后支持6位
2:  ptime pt1(time_from_string(ts1));
3:  std::string ts2("20130130T233222");//没有分隔符的date和time
4:  ptime pt2(from_iso_string(ts2));
5:  

3.通过时钟构造ptime:

1:      ptime ct1(second_clock::local_time());
2:  ptime ct2(second_clock::universal_time());
3:  ptime ct3(microsec_clock::local_time());
4:  ptime ct4(microsec_clock::universal_time());
5:  

4.time_t和FILETIME构造ptime:

1:      ptime t = from_time_t(tt); // 其中tt为time_t
2:  ptime t1 = from_ftime<ptime>(ft); //其中ft为FILETIME

ptime访问日期时间

1:      using namespace boost::posix_time;
2:      using namespace boost::gregorian;
3:  ptime now(second_clock::local_time());
4:  std::cout << "today is: " << now.date() << std::endl;
5:  std::cout << "time is: " << now.time_of_day() << std::endl;
6:  

ptime转换为string

1:      std::string now_str(to_simple_string(now));
2:  std::string now_iso_str(to_iso_string(now));
3:  std::string now_iso_ext_str(to_iso_extended_string(now));
4:  std::cout << now_str << std::endl;
5:  std::cout << now_iso_str << std::endl;
6:  std::cout << now_iso_ext_str << std::endl;

ptime与tm,time_t,FILETIME互转

1.tm

 1:     using namespace boost::posix_time;
 2:     using namespace boost::gregorian;
 3:     tm pt_tm;
 4:     pt_tm.tm_year = 113;
 5:     pt_tm.tm_mon = 11;
 6:     pt_tm.tm_mday = 25;
 7:     pt_tm.tm_hour = 2;
 8:     pt_tm.tm_min = 23;
 9:     pt_tm.tm_sec = 40;
10:  
11:     ptime pt = data_from_tm(pt_tm);
12:     std::cout << pt << std::endl;
13:  
14:     pt = pt + hours(2);
15:     tm pt_tm1 = to_tm(pt);

2. time_t

 1:      using namespace boost::posix_time;
 2:      using namespace boost::gregorian;
 3:  
 4:  time_t now = time(NULL);
 5:  std::cout << "time_t : " << now << std::endl;
 6:  ptime now_pt = from_time_t(now);
 7:  std::cout << "ptime from time_t : " << now_pt.time_of_day() << std::endl;
 8:  tm* now_tm = gmtime(&now);
 9:  std::cout << "tm struct: hour : " << now_tm->tm_hour << std::endl;
10:  

3.FILETIME

1:      FILETIME ft;
2:  ft.dwHighDateTime = 29715317;
3:  ft.dwLowDateTime = 3865122988UL
4:      ptime pt = from_ftime<ptime>(ft);
5:  // pt ===> 2005-Jun-07 15:30:57.03958200
6:  

time_duration和time_period

 1:      using namespace boost::posix_time;
 2:      using namespace boost::gregorian;
 3:  
 4:  time_duration td(100,200,3,9);
 5:  std::cout << td << std::endl;
 6:  date d(2013,Feb,5);
 7:  ptime pt(d,minutes(10));
 8:  ptime pt1(d,hours(10));
 9:  time_period tp(pt,pt1);
10:  std::cout << tp << std::endl;
11:  

对于这两者的区别,一个是时间间隔,一个是时间起止的一个窗口。time_duration用于ptime的时间偏移计算为主。而time_period可以计算一个ptime时间点是否在这个时间区间内(参考contains函数)。time_period在创建之后可以扩展,可以平移,函数分别为expand和shift。请大家自己细究。


0 0
原创粉丝点击