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++标准委员会 ...
随机推荐
- foreach on Request.Files
https://stackoverflow.com/questions/1760510/foreach-on-request-files The enumerator on the HttpFileC ...
- HDU 5634 Rikka with Phi
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 ------------------------------------------------ ...
- linux RZ 命令
root 账号登陆后,依次执行以下命令: 1 cd /tmp 2 wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz 3 tar zxv ...
- Vagrant - 打造跨平台的一致开发环境
官网 参考资料 借助 Vagrant ,可以使用 Vagrantfile 文件自动化虚拟机的安装和配置流程,方便快速的打造跨平台的统一开发环境. 1. Vagrant 是啥 Vagrant 用于构建及 ...
- vue- 指令v-if 与指令v-show的区别
区别1: v-if :可以根据表达式的值在DOM中生成或移除一个元素. v-show:可以根据表达式的值来显示或者隐藏HTML元素.当v-show赋值为false时,元素被隐藏,此时查看代码时,该元素 ...
- poj3614Sunscreen
Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her ...
- POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料
如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮 ...
- 基于Select模型的Windows TCP服务端和客户端程序示例
最近跟着刘远东老师的<C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台)>,Bilibili视频地址为C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台),重新复习下 ...
- 在LIPS表追加拣配数量PIKMG字段(转)
原文地址:https://blog.csdn.net/zhongguomao/article/details/43451127 最近比较忙,此方案出后测试了很多种情况都存在问题,只能留待以后处理了.. ...
- File类的相关方法
java.io.File类 文件和路径名的抽象表达形式 java把电脑中的文件和文件夹(目录)封装了一个File类,我们可以使用File类对文件和文件夹进行如下操作 创建一个文件/文件夹 删除 获取 ...