码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-27 12:35:47      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:os   使用   io   strong   ar   div   amp   sp   时间   

<开篇>

本篇紧接着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.datetime的格式化输入输出。

<完结>

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

标签:os   使用   io   strong   ar   div   amp   sp   时间   

原文地址:http://www.cnblogs.com/lidabo/p/3938969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!