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. 微信小程序的开发框架

    wxss:是一套样式语言,用来描述wxml的组件样式:小程序在css基础上做的修改和扩充的版本 css:是一套样式语言,样式表,用来描述xml和html文件样式的呈现: 设备像素:是图片在设备上显示的 ...

  2. 123、TensorFlow的Job

    # 如果你在分布式环境中部署TensorFlow # 你或许需要指定job name和task ID # 来将变量放置在参数服务器上 # 将操作放在worker job import tensorfl ...

  3. Mybatis入门之MyBatis基础

    一.MyBatis概述 1.ORM模型简介 ORM:对象关系映射(Object Relation Mapping) 1)传统JDBC程序的设计缺陷(实际项目不使用) a.大量配置信息硬编码 b.大量的 ...

  4. C++ 测量程序执行时间的办法

    #include <time.h> clock_t start = clock(); //时间起始 /*待测试代码*/ clock_t end = clock(); //时间测试结束 co ...

  5. memcpy复制字符串的注意事项/memcpy不能用来拷贝类类型

    strcpy复制src到dst,最后将dst的下一个位置置为'\0',所以dst是以'\0'结尾的字符串 ] = "abcde"; cout << c1 << ...

  6. Buy Tickets 【POJ - 2828】【线段树】

    题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而 ...

  7. SQL计算两个时间段相隔时间

    SQL语句: select cast(floor(datediff(minute,时间1,时间2) / 1440) as varchar)+'天'+ cast(floor((datediff(minu ...

  8. ajax基础--基本概念

    1.Ajax的全称: Asynchronous Javascript And XML,就是使用is代码获取服务器数据 局部异步刷新 IP地址:用来标识查找某一台计算机 域名:ip地址太难记了,使用域名 ...

  9. 07 (H5*) js课程第8天 高阶函数、闭包、沙箱

    目录: 1:call和apply方法调用 2:bind方法复制 3:函数中的几个属性值 4:高阶函数,函数作为参数 5:高阶函数,函数作为返回值. 6:   作用域链,作用域,预解析 7:闭包--延长 ...

  10. phpcms批量更新内容页只更新一点就返回问题

    phpcms批量更新内容页只更新一点就返回问题 给caches目录增加写入权限