[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。请大家自己细究。
下一篇将介绍关于boost.datetime的格式化输入输出。
<完结>
[Boost]boost的时间和日期处理-(2)时间的操作的更多相关文章
- C标准函数库中获取时间与日期、对时间与日期数据操作及格式化
表示时间的三种数据类型[编辑] 日历时间(calendar time),是从一个标准时间点(epoch)到现在的时间经过的秒数,不包括插入闰秒对时间的调整.开始计时的标准时间点,各种编译器一般使用19 ...
- ios 如何获得系统时间和日期
iphone 如何获得系统时间和日期 代码如下: #import <time.h> 1.获得当前的系统时间和日期 //获得系统时间 NSDate * senddate=[NSDate d ...
- [Boost]boost的时间和日期处理-(1)日期的操作
<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...
- boost 时间与日期处理
博客转载自: 类 特点 缺点 说明 timer 计时基类 不适合大跨度时间 适用大部分的普通计时 progress_timer 继承自timer 可以自动写入流中 只精确到0.01s 如果需要更精确, ...
- Android随笔之——Android时间、日期相关类和方法
今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...
- postgreSQL时间、日期函数
一.获取系统时间函数 1.1.获取当前完整时间 select now(); select current_timestamp; 1.2.获取当前日期 select currnt_date: 1.3.获 ...
- Lua库之时间和日期操作
Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...
- yii2超好用的日期组件和时间组件
作者:白狼 出处:http://www.manks.top/yii2_datetimepicker.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接 ...
- date 显示或设置系统时间和日期
显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...
随机推荐
- Oracle表解锁
网搜 --第一步 查看被锁表 select b.owner,b.object_name, b.object_id,l.session_id,l.locked_mode from v$locked_ob ...
- 解决phpmyadmin配置文件的权限问题
如果部署的phpmyadmin权限为所有人可写,即权限为777,就会报”Wrong permissions on configuration file, should not be world wri ...
- 2013 南京邀请赛 K题 yet another end of the world
/** 大意:给定一组x[],y[],z[] 确定有没有两个不同的x[i], x[j] 看是否存在一个ID使得 y[i]<=ID%x[i]<=z[i] y[j]<=ID%x[j]&l ...
- C++ ofstream和ifstream详细用法
转载地址:http://soft.chinabyte.com/database/460/11433960.shtml ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就 ...
- MFC技术内幕系列之(四)---MFC消息映射与消息传递内幕
//////////////////////////////////////////////////////////////////////////////////// ...
- English learning method ---学英语重中之重打通“任督二脉”
漫漫十年艰辛路,英语学习之旅 曾经秉承“路漫漫其修远兮,吾将上下而求索”的信念,初一那年了解到原来(a b c d e f g) 不仅仅读作(啊,波,词,的,额,佛,哥),在英语的世界中它有另外的读法 ...
- Phalcon框架中的另类使用
不像传统的PHP框架,假设框架想被还有一个框架使用仅仅能通过rpc或是引入文件等的方式.Phalcon能够在其他框架中直接使用.这是因为Phalcon是以扩展的形式存在的,在server载入时会直接载 ...
- c.Tom and paper
Tom and paper Description There is a piece of paper in front of Tom, its length and width are intege ...
- double 型变量的输入输出标准格式
c语言double型变量标准输入格式: scanf("%lf",num); 标准输出格式: printf("%f\n",num); 注:有过输出用%lf输出OJ ...
- Raphael入门实例:动画与箭头
raphael 实例 动画 隐藏和显示参数说明 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 var c = paper.circle(50, 50, 40); function ...