将非标准化间隔分成五分钟的算法 - Python / SAS

Algorithm to break non-standardized intervals into five minute periods - Python / SAS

假设我有一个输入 (22:00 - 43:00)。我想从中创建以下内容:

begin = 22:00 end = 25:00
begin = 25:00 end = 30:00
begin = 30:00 end = 35:00
begin = 35:00 end = 40:00
begin = 40:00 end = 43:00

如何使用循环以最少的代码(包括部分第一个和最后一个间隔)高效地获取新的开始日期和新的结束日期?棘手的部分是,如果给定时间间隔,如 (20:00 - 43:00) 或 (22:00 - 40:00) 或简单地 (20:00 - 40:00,我需要解决方案才能工作).

将在 SAS 中对日期时间变量实施此操作,但非常感谢 Python 中的算法,因为我知道 SO 上有更多用户。

谢谢。

这是一个 SAS 解决方案。 5 分钟对应 300 秒,SAS 中的日期时间以秒为单位。变量以日期时间表示,但如果您只需要时间,则可以使用 timepart() 函数。

*Generate some sample data;
data have;
time_s = dhms('01Jan2014'd, 0, 24, 0);
time_e = dhms('01Jan2014'd, 0, 40, 0);
format time: datetime20.;
run;

proc print data=have;
run;

*Calculate intervals;
data want;
set have;

time_start=time_s;
time_end=round(time_s+150, 300);

output;

do while (time_end<time_e);
    time_start=time_end;
    time_end+300;

    if time_end>time_e then time_end=time_e;
    output;
end;

format time: datetime20.;
run;

proc print data=want;
run;

并且输出:

                  Obs              time_start                time_end

                   1       01JAN2014:00:22:00      01JAN2014:00:25:00
                   2       01JAN2014:00:25:00      01JAN2014:00:30:00
                   3       01JAN2014:00:30:00      01JAN2014:00:35:00
                   4       01JAN2014:00:35:00      01JAN2014:00:40:00
                   5       01JAN2014:00:40:00      01JAN2014:00:43:00

SAS有intnx() function for incrementing by date and time intervals. This approach uses intnx() alongside the intck() function to count the intervals and the minutes5 interval for alignment. Additionally, the ifn() function用来判断是否使用起始值和结束值。

data have;
    start = dhms("01Jan1960"d, 0, 23, 0);
    end = dhms("01Jan1960"d, 0, 51, 0);
run;
data want;
    set have;
    /* Get number of intervals to loop over */
    max = intck("minute5", start, end - 1) + 1;
    do i = 1 to max;
        /* Offset start date by the loop number of intervals */
        time_start = ifn(i = 1, start, intnx("minute5", start, i - 1 , "b"));
        time_end = ifn(i = max, end, intnx("minute5", start, i, "b"));
        output;
    end;
    format time: datetime.;
    keep time:;
run;