时间点ptime就是日期(date)加上一个不超过24小时的时间长度(time_duration)。
#include <iostream>
#include <boost\date_time\gregorian\gregorian.hpp>
#include <boost\date_time\posix_time\posix_time.hpp>
using namespace std;
using namespace boost;
int main() {
//创建键日期
gregorian::date d = gregorian::day_clock::local_day();
//创建1小时的时间长度
posix_time::time_duration td(1,0,0,0);
//使用日期和时间长度创建时间点
posix_time::ptime p(d,td);
cout << p << endl;
//时间点运算
posix_time::hours h(3);
posix_time::ptime p1 = p + h;
cout << p1 << endl;
cout << posix_time::to_iso_extended_string(p1) << endl;
assert(p < p1);
//和tm之间的转换
tm t = posix_time::to_tm(p);
posix_time::ptime p2 = posix_time::ptime_from_tm(t);
//时间点迭代器
posix_time::time_iterator timeIter(p,posix_time::minutes(20));
while (timeIter < p + posix_time::hours(1)) {
cout << *timeIter << endl;
++timeIter;
}
return 0;
}
2018-Jun-21 01:00:00
2018-Jun-21 04:00:00
2018-06-21T04:00:00
2018-Jun-21 01:00:00
2018-Jun-21 01:20:00
2018-Jun-21 01:40:00