如何以 5 分钟为单位生成 DateTime 范围?
How to generate a DateTime range by step of 5 minutes?
如何生成一个以5分钟为单位的DateTime范围?以下是输出:
2022-02-13 00:00:00, 2022-02-13 00:05:00
2022-02-13 00:05:00, 2022-02-13 00:10:00
2022-02-13 00:10:00, 2022-02-13 00:15:00
2022-02-13 00:15:00, 2022-02-13 00:20:00
...
2022-02-13 23:45:00, 2022-02-13 23:50:00
2022-02-13 23:50:00, 2022-02-13 23:55:00
2022-02-13 23:55:00, 2022-02-14 00:00:00
使用Interval模块。
use Interval;
my $dt = Interval[300].new(time);
say $dt++ for ^5;
2022-02-13T15:29:03Z
2022-02-13T15:34:03Z
2022-02-13T15:39:03Z
2022-02-13T15:44:03Z
2022-02-13T15:49:03Z
# when to start when to finish
my $start = DateTime.now.truncated-to("day");
my $end = $start.later(:1day);
# after start, add 5 mins repeatedly, and stop when end is exceeded
say $start, *.later(:5minutes) ... * >= $end;
my $dt = Date.today.DateTime;
$dt.clone: formatter => { .yyyy-mm-dd ~ q{ } ~ .hh-mm-ss }\
andthen $_, *.later(:5minute) ... * eqv $dt.later(:1day)
andthen .rotor: 2 => -1
andthen .map: *.join(q{, }).put
我想出了另一个解决方案:
sub MAIN() {
my $fmt = {sprintf "%04d-%02d-%02d %02d:%02d:%02d", .year, .month, .day, .hour, .minute, .second};
my @dates = gather given Date.today {
my $current-datetime = .DateTime;
while $current-datetime <= .succ.DateTime {
take $current-datetime;
$current-datetime = $current-datetime + Duration.new(300.0);
}
}
for @dates.rotor(2 => -1) {
printf("%s, %s\n", .[0].clone(formatter => $fmt), .[1].clone(formatter => $fmt));
}
}
感谢@wamba 提供 clone
方法。
如何生成一个以5分钟为单位的DateTime范围?以下是输出:
2022-02-13 00:00:00, 2022-02-13 00:05:00
2022-02-13 00:05:00, 2022-02-13 00:10:00
2022-02-13 00:10:00, 2022-02-13 00:15:00
2022-02-13 00:15:00, 2022-02-13 00:20:00
...
2022-02-13 23:45:00, 2022-02-13 23:50:00
2022-02-13 23:50:00, 2022-02-13 23:55:00
2022-02-13 23:55:00, 2022-02-14 00:00:00
使用Interval模块。
use Interval;
my $dt = Interval[300].new(time);
say $dt++ for ^5;
2022-02-13T15:29:03Z
2022-02-13T15:34:03Z
2022-02-13T15:39:03Z
2022-02-13T15:44:03Z
2022-02-13T15:49:03Z
# when to start when to finish
my $start = DateTime.now.truncated-to("day");
my $end = $start.later(:1day);
# after start, add 5 mins repeatedly, and stop when end is exceeded
say $start, *.later(:5minutes) ... * >= $end;
my $dt = Date.today.DateTime;
$dt.clone: formatter => { .yyyy-mm-dd ~ q{ } ~ .hh-mm-ss }\
andthen $_, *.later(:5minute) ... * eqv $dt.later(:1day)
andthen .rotor: 2 => -1
andthen .map: *.join(q{, }).put
我想出了另一个解决方案:
sub MAIN() {
my $fmt = {sprintf "%04d-%02d-%02d %02d:%02d:%02d", .year, .month, .day, .hour, .minute, .second};
my @dates = gather given Date.today {
my $current-datetime = .DateTime;
while $current-datetime <= .succ.DateTime {
take $current-datetime;
$current-datetime = $current-datetime + Duration.new(300.0);
}
}
for @dates.rotor(2 => -1) {
printf("%s, %s\n", .[0].clone(formatter => $fmt), .[1].clone(formatter => $fmt));
}
}
感谢@wamba 提供 clone
方法。