加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

boost location-independent times

发布时间:2020-12-16 10:47:23 所属栏目:百科 来源:网络整理
导读: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/gregori

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,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,0));
  ptime pt2(date(2014,time_duration(18,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,0));
  time_iterator it(pt,time_duration(6,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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读