如何在提升日期时间中创建日期范围?

时间:2018-06-19 11:24:53

标签: c++ datetime boost

我想创建一个时间戳范围,介于开始日期和结束日期以及选定的频率之间。例如,给定以下参数作为开始日期,结束日期和频率:

2002-01-20 23:59:59.000
2002-01-21 04:59:59.000
Freq = Hour

它应该返回一个向量/时间戳列表:

2002-01-21 00:00:00.000
2002-01-21 01:00:00.000
2002-01-21 02:00:00.000
2002-01-21 03:00:00.000
2002-01-21 04:00:00.000

Boost:date_time库是否具有实现此功能的功能?

1 个答案:

答案 0 :(得分:3)

Boost有两个相关的内容:

第一个字面值给您一个日期范围。第二点是我在寻找的东西。

library(rgl) x <- y <- seq(-1, 1, len=20) x <- x + 0.5 # to distinguish it from y z <- outer(x, y, function(x,y) x^2 + y^2) col <- rainbow(10)[cut(z, breaks = 10)] surface3d(x, y, z, color = col)

示例:

time_period

打印

#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>

using namespace boost::posix_time;
using namespace boost::gregorian;

struct day_period : time_period {
    day_period(date d) : time_period(ptime(d), ptime(d, hours(24))) {}
};

int main() {
    const date d(2002, Feb, 1); // an arbitrary date
    const day_period dp(d); // the containing day

    ptime t(d, hours(3) + seconds(5)); // an arbitray time on that day

    if (dp.contains(t)) {
        std::cout << to_simple_string(dp) << " contains " << to_simple_string(t) << std::endl;
    }

    // a period that represents part of the day
    time_period part_of_day(ptime(d, hours(0)), t);

    // intersect the 2 periods and print the results
    if (part_of_day.intersects(dp)) {

        time_period result = part_of_day.intersection(dp);

        std::cout 
            << to_simple_string(dp) << " intersected with\n"
            << to_simple_string(part_of_day) << " is \n"
            << to_simple_string(result) << std::endl;
    }
}

时间迭代器

示例:

[2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999]
contains 2002-Feb-01 03:00:05
[2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999]
intersected with
[2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]
is
[2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]