boost location-independent times
The class boost::posix_time::ptime defindes a location-independent time. It uses the type boost::gregorian::date, but also stores a time.
1. ptime
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream> using namespace boost::posix_time;
using namespace boost::gregorian; int main()
{
ptime pt(date(, , 12), time_duration(, , 0));
date d = pt.date();
std::cout << d << std::endl;
time_duration td = pt.time_of_day();
std::cout << td << std::endl;
ptime pt2 = second_clock::universal_time();
std::cout << pt2.date() << std::endl;
std::cout << pt2.time_of_day() << std::endl;
pt2 = from_iso_string("20140512T120000");
std::cout << pt2.date() << std::endl;
std::cout << pt2.time_of_day() << std::endl;
return ; }
To initialize an object of type boost::posix_time::ptime, pass a date of type boost::gregorian::date and a duration of type boost::posix_time::time_duration as the first and second parameters to the constructor. The constructor of boost::posix_time::time_duration takes three parameters, which determine the time. To query date and time, use the member functions date() and time_of_day().
The class boost::posix_time::second_clock returns the current time. The member function universal_time() returns the UTC time. local_time() returns the local time. The free-standing function boost::posix_time::from_iso_string() converts a time stored in a string formatted using the ISO 8601 standard into an object of type.
2. time_duration
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream> using namespace boost::posix_time; int main()
{
time_duration td(, , 0);
std::cout << td.hours() << std::endl;
std::cout << td.minutes() << std::endl;
std::cout << td.seconds() << std::endl;
std::cout << td.total_seconds() << std::endl;
ptime pt1{date{2014, 5, 12}, time_duration{12, 0, 0}};
ptime pt2{date{2014, 5, 12}, time_duration{18, 30, 0}};
time_duration td2 = pt2 - pt1;
std::cout << td2.hours() << std::endl;
std::cout << td2.minutes() << std::endl;
std::cout << td2.seconds() << std::endl;
return ; }
hours(), minutes() and seconds return the respective parts of a time duration, while member functions such as total_seconds(), which returns the total number of seconds.
If two times of type boost::posix_time::ptime are subtracted from each other, the result in an object of type boost::posix_time::time_duration that specifies the duration between the two times.
3. time_period
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream> using namespace boost::posix_time;
using namespace boost::gregorian; int main()
{
ptime pt1(date(, , 12), time_duration(, , 0));
ptime pt2(date(, , 12), time_duration(, , 0));
time_period tp{pt1, pt2};
std::cout.setf(std::ios::boolalpha);
std::cout << tp.contains(pt1) << std::endl;
std::cout << tp.contains(pt2) << std::endl;
return ;
}
In general, boost::posix_time::time_period works just like boost::gregorian::date_period. It provides a member function, contains(), which returns true for every point in time within the period. Because the end time, which is passed to the constructor of boost::posix_time::time_period, is not part of the period, the second call to contains() in example above returns false.
4. iterator
#include <boost/date_time/local_time/local_time.hpp>
#include <iostream> using namespace boost::posix_time;
using namespace boost::gregorian; int main()
{
ptime pt(date(, , 12), time_duration(, , 0));
time_iterator it(pt, time_duration(, , 0));
std::cout << *++it << std::endl;
std::cout << *++it << std::endl;
return ;
}
Example above uses the iterator it to jump forward 6.5 hours from the time pt. Because the iterator is increamented twice,, the output is 2014-May-12 18:30:00 and 2014-May-13 01:00:00.
boost location-independent times的更多相关文章
- cvpr2015papers
@http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...
- RIP 相对寻址
知识共享许可协议本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/rip-relative-addressing 本博客 ...
- [Windows Azure] Adding Sign-On to Your Web Application Using Windows Azure AD
Adding Sign-On to Your Web Application Using Windows Azure AD 14 out of 19 rated this helpful - Rate ...
- Optimizing subroutine calls based on architecture level of called subroutine
A technique is provided for generating stubs. A processing circuit receives a call to a called funct ...
- Linking code for an enhanced application binary interface (ABI) with decode time instruction optimization
A code sequence made up multiple instructions and specifying an offset from a base address is identi ...
- 从机器码理解RIP 相对寻址
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/rip-relative-addressing 本博客同步在 ...
- 华为云计算IE面试笔记-其它知识点
* 虚拟化与云计算的区别?云计算和虚拟化关系?华为云计算怎么做的? 虚拟化是一种具体的技术,实现的是对硬件资源的虚拟化,提升资源利用率,降低能耗,云计算是一种概念各种技术组件的集合,针对的是对各种资源 ...
- vs2008编译boost
vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库 ...
- Boost 1.62.0 编译参数
# Copyright Vladimir Prus 2002-2006.# Copyright Dave Abrahams 2005-2006.# Copyright Rene Rivera 2005 ...
- 编译Boost 详细步骤
vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会 ...
随机推荐
- windows连接ubuntu服务器方式
如图,打开cmd, 输入 ssh imkow@www.dorian.vip 参数解析: ssh:secure shell的缩写 imknow 是用户名 www.dorian.vip 是域名,没有域名 ...
- python中将12345转换为54321
#将12345转换为54321 a = 12345789 ret = 0 #当a不为零的时候,循环条件为true,执行语句块 while a : #对a求余数,第一次循环则把5求出来 last = a ...
- (转)YAML最最基础语法
转:https://blog.csdn.net/vincent_hbl/article/details/75411243 正如YAML所表示的YAML Ain’t Markup Language,YA ...
- 麦子lavarel---16、日志
麦子lavarel---16.日志 一.总结 一句话总结: 一定要养成打印日志查看日志的好习惯,非常节约时间和便于查错 1.console.Log(“获取类别数据:"):console.Lo ...
- DB-MDM:MDM/主数据管理 百科
ylbtech-DB-MDM:MDM/主数据管理 百科 主数据管理(MDM Master Data Management)描述了一组规程.技术和解决方案,这些规程.技术和解决方案用于为所有利益相关方( ...
- 【OpenCV】 在CentOS下搭建OpenCV开发环境
最近开始入模式识别的坑,自然被迫上OpenCV了. 在多次尝试给VS2015扩展Windows 10 SDK无果后(不要问我为啥..VS2015开发C++的标准库全给扔到这个SDK里了,打包在VS安装 ...
- python web自动化测试框架搭建(功能&接口)——功能测试模块
功能测试使用selenium,模块有: 1.futil: 公共方法,如元素高亮显示 # coding=utf-8 """高亮显示元素""" ...
- spring-boot BUG 集锦
BUG1: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. BUG2:使用 ...
- spring容器、spring MVC容器以及web容器的区别
本文转载自 https://www.cnblogs.com/xiexin2015/p/9023239.html 说到spring和springmvc,其实有很多工作好多年的人也分不清他们有什么区别,如 ...
- MySQL- 查询总结
查询总结 语法: select 查询字段 from 表 别名 连接类型inner|left|right join on 连接条件 where 筛选 group by 分组列表 having 筛选(二次 ...