boost location-independent times

本文详细介绍了Boost库中时间日期处理的相关类和函数,包括ptime、time_duration、time_period和迭代器的使用方法,展示了如何初始化ptime对象、计算时间间隔、判断时间范围以及进行时间迭代。

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(2014, 5, 12), time_duration(12, 0, 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 0; }

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(16, 30, 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 0; }

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(2014, 5, 12), time_duration(12, 0, 0));
  ptime pt2(date(2014, 5, 12), time_duration(18, 30, 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 0;
}

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(2014, 5, 12), time_duration(12, 0, 0));
  time_iterator it(pt, time_duration(6, 30, 0));
  std::cout << *++it << std::endl;
  std::cout << *++it << std::endl;
  return 0;
}

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.

转载于:https://www.cnblogs.com/sssblog/p/11304192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值