Boost学习笔记——Date_Time

 boost::gregorian::date是Boost中最主要的时间类。下面几个例子说明了其简洁性。

 1 #include "boost/date_time/gregorian/gregorian.hpp"
 2 #include <iostream>
 3 
 4 int
 5 main() 
 6 {
 7   
 8   using namespace boost::gregorian;
 9   std::string s;
10   std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): ";
11   std::cin >> s;
12   try {
13     date birthday(from_simple_string(s));
14     date today = day_clock::local_day();
15     days days_alive = today - birthday;
16     days one_day(1);
17     if (days_alive == one_day) {
18       std::cout << "Born yesterday, very funny" << std::endl;
19     }
20     else if (days_alive < days(0)) {
21       std::cout << "Not born yet, hmm: " << days_alive.days() 
22                 << " days" <<std::endl;
23     }
24     else {
25       std::cout << "Days alive: " << days_alive.days() << std::endl;
26     }
27 
28   }
29   catch(...) {
30     std::cout << "Bad date entered: " << s << std::endl;
31   }
32   return 0;
33 }

头文件:

#include "boost/date_time/gregorian/gregorian.hpp"

相关成员函数参阅:

http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/gregorian.html 

时间段的相关操作:

http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/gregorian.html#date_time.gregorian.date_duration

 

posted @ 2013-09-18 11:23  鬞鬤  阅读(270)  评论(0编辑  收藏  举报