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

    w PowerDesigner

  2. django中的url控制

    1.django中的第一个控件:url控制  (路由分发) urls.py:请求路径与视图函数的之间的关系 步骤: 1.首先是要配置环境,   2.其次就是引路径   3.在视图的文件夹里面写相应的函 ...

  3. The server time zone value 'EDT' is unrecognized or represents more than one time zone

    解决: (1)使用 server mysql start命令启动mysql (2)在mysql中执行show variables like '%time_zone%'; (3)输入select now ...

  4. jmeter之cookies登录

    现在很多网站的登录都要验证码了,验证码的值是动态的,值不易获取.使用jmeter测试一个需要登录的接口就有困难,这时候,我们就可以使用cookies管理器来记住这个登录信息. 目录 1.jmeter的 ...

  5. Looper,Handler, MessageQueue

    Looper Looper是线程用来运行消息循环(message loop)的类.默认情况下,线程并没有与之关联的Looper,可以通过在线程中调用Looper.prepare() 方法来获取,并通过 ...

  6. linux下的sleep()和usleep()的使用和区别

    函数名: sleep头文件: #include<windows.h> // 在VC中使用带上头文件              #include<unistd.h>    // ...

  7. vuejs基础-常见指令(基本结构、v-cloak、v-text、v-html、v-bind、v-model\v-if、v-show)

    Vue之 - 基本的代码结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  8. Hibernate异常:IllegalArgumentException

    异常信息: java.lang.IllegalArgumentException: attempt to create delete event with null entity at org.hib ...

  9. java序列化的相关介绍

    1.什么是序列化?为什么要用序列化? 序列化就是将对象状态转换为可保持或传输的格式的过程.与序列化相对的就是反序列化,他将流转换成对象.这两个过程结合起来,可以轻松地存储和传输数据. 注意:对象序列化 ...

  10. Python入门习题4.文本进度条

    例4.1.设置一组文本进度条,使之运行效果如下: --------执行开始--------% 0 [->**********]%10 [*->*********]%20 [**->* ...