首页 技术 正文
技术 2022年11月11日
0 收藏 407 点赞 4,327 浏览 2662 个字

<开篇>

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

<完结>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905