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的更多相关文章

  1. cvpr2015papers

    @http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...

  2. RIP 相对寻址

    知识共享许可协议本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/rip-relative-addressing 本博客 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 从机器码理解RIP 相对寻址

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/rip-relative-addressing 本博客同步在 ...

  7. 华为云计算IE面试笔记-其它知识点

    * 虚拟化与云计算的区别?云计算和虚拟化关系?华为云计算怎么做的? 虚拟化是一种具体的技术,实现的是对硬件资源的虚拟化,提升资源利用率,降低能耗,云计算是一种概念各种技术组件的集合,针对的是对各种资源 ...

  8. vs2008编译boost

    vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库 ...

  9. Boost 1.62.0 编译参数

    # Copyright Vladimir Prus 2002-2006.# Copyright Dave Abrahams 2005-2006.# Copyright Rene Rivera 2005 ...

  10. 编译Boost 详细步骤

    vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会 ...

随机推荐

  1. 腾讯云服务器centos通过yum安装mysql数据库

    安装mysql有两种: 1-可以使用yum安装, 2-可以自己下载安装包安装mysql, 腾讯云的centos系统自带了yum,所以用yum安装方便点 安装步骤 1-查看yum源中是否有mysql y ...

  2. php面试专题---21、MVC框架基本工作原理考察点

    php面试专题---21.MVC框架基本工作原理考察点 一.总结 一句话总结: 会的东西快速过,不要浪费时间,生命有限,都是一些很简单的东西. 1.mvc框架单一入口的 优势 是什么? 可以进行统一的 ...

  3. maven将依赖第三方包打包(package)到jar中

    前提:项目是一个纯maven的java工程,通过idea中file-->new-->project-->maven来创建的,不是spring boot工程(不是通过file--> ...

  4. java.io.NotSerializableException错误解决方法

    运行tomcat下面的 ssh项目,启动,打开某页面(让session起作用),停止:再启动,有可能会报类似如下的错误: org.apache.catalina.session.StandardMan ...

  5. servlet反射、生命周期、接口

    什么是Servlet Servlet是JavaWeb的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要: l  ...

  6. 什么是php扩展

    PHP扩展英文为PHP Extension and Application Repository,简称pear(下面都以pear简称),中文全称为PHP扩展与应用库.是为了创建一个类似于Perl CP ...

  7. TCP协议分析(包结构)---转

    TCP首部格式 tcp数据是被封装在IP数据包中的,和udp类似,在IP数据包的数据部分.tcp数据包的格式如下: 源端口号和目的端口号(寻址)与udp中类似,用于寻找发端和收端应用进程.这两个值加上 ...

  8. Codeforces 1105C (DP)

    题面 传送门 分析 这种计数问题,要不是纯数学推公式,要不就是dp 先处理出[l,r]中除3余0,1,2的数的个数,记为cnt0,cnt1,cnt2 设\(dp[i][j]\)表示前i个数的和除3余j ...

  9. 17、NumPy——副本和视图

    副本是一个数据的完整的拷贝,如果我们对副本进行修改,它不会影响到原始数据,物理内存不在同一位置. 视图是数据的一个别称或引用,通过该别称或引用亦便可访问.操作原有数据,但原有数据不会产生拷贝.如果我们 ...

  10. 6、numpy——高级索引

    NumPy 比一般的 Python 序列提供更多的索引方式.除了之前看到的用整数和切片的索引外,数组可以由整数数组索引.布尔索引及花式索引. 1.整数数组索引 1.1 以下实例获取数组中(0,0),( ...