鉴于以下DateTime::SpanSet
包含6个DateTime::Span
个对象(每个范围打印start
和end
DateTime
),我想将其反转并限制它一个跨度。
[2013-02-04T00:00:00 -> 2013-02-04T03:00:00]
[2013-02-04T07:30:00 -> 2013-02-04T12:00:00]
[2013-02-04T12:45:00 -> 2013-02-04T16:45:00]
[2013-02-05T07:30:00 -> 2013-02-05T16:45:00]
[2013-02-06T08:00:00 -> 2013-02-06T16:30:00]
[2013-02-07T16:00:00 -> 2013-02-08T16:30:00]
使用complement
反转它,提供以下内容:
[-inf -> 2013-02-04T00:00:00]
[2013-02-04T03:00:00 -> 2013-02-04T07:30:00]
[2013-02-04T12:00:00 -> 2013-02-04T12:45:00]
[2013-02-04T16:45:00 -> 2013-02-05T07:30:00]
[2013-02-05T16:45:00 -> 2013-02-06T08:00:00]
[2013-02-06T16:30:00 -> 2013-02-07T16:00:00]
[2013-02-08T16:30:00 -> inf]
我正在寻找的最终结果应受DateTime::Span
的{{1}}限制,给我以下结果:
2013-02-04T00:00:00 -> 2013-02-11T00:00:00
我可以通过以下方式实现这一目标:
[2013-02-04T03:00:00 -> 2013-02-04T07:30:00]
[2013-02-04T12:00:00 -> 2013-02-04T12:45:00]
[2013-02-04T16:45:00 -> 2013-02-05T07:30:00]
[2013-02-05T16:45:00 -> 2013-02-06T08:00:00]
[2013-02-06T16:30:00 -> 2013-02-07T16:00:00]
[2013-02-08T16:30:00 -> 2013-02-11T00:00:00]
是否有可能以更好的方式做到这一点?
答案 0 :(得分:0)
对此的解决方案似乎太容易了,我实际上构建了一个测试工具来根据我的结果验证您的结果,我在下面已经包含了这些结果。我的Perl有点生疏,但这是我提出的解决方案:
sub alternateInvert {
my $spanset = shift;
my $boundary = shift;
return $boundary->complement($spanset);
}
使用这个给了我与你的功能相同的结果。 这是测试工具:
#!/opt/local/bin/perl
use DateTime::Span;
use DateTime::Format::RFC3339;
use DateTime;
use strict;
use warnings;
sub invertSpansetByBoundary {
my $spanset = shift;
my $boundary = shift;
$spanset = $spanset->complement;
my $iter = $spanset->iterator(span => $boundary);
my $ss = DateTime::SpanSet->empty_set();
while(my $timespan = $iter->next) {
$ss = $ss->union($timespan);
}
return $ss;
}
sub alternateInvert {
my $spanset = shift;
my $boundary = shift;
return $boundary->complement($spanset);
}
sub printSpanset {
my $spanset = shift;
my $iter = $spanset->iterator(span => $spanset);
while(my $timespan = $iter->next) {
printf "[%s -> %s]\n", $timespan->start, $timespan->end;
}
}
my $datestrs = [
["2013-02-04T00:00:00Z", "2013-02-04T03:00:00Z"],
["2013-02-04T07:30:00Z", "2013-02-04T12:00:00Z"],
["2013-02-04T12:45:00Z", "2013-02-04T16:45:00Z"],
["2013-02-05T07:30:00Z", "2013-02-05T16:45:00Z"],
["2013-02-06T08:00:00Z", "2013-02-06T16:30:00Z"],
["2013-02-07T16:00:00Z", "2013-02-08T16:30:00Z"]];
my $spanset = DateTime::SpanSet->empty_set();
my $format = DateTime::Format::RFC3339->new();
foreach my $set (@$datestrs) {
my $startdt = $format->parse_datetime($set->[0]);
my $enddt = $format->parse_datetime($set->[1]);
$spanset = $spanset->union(DateTime::Span->from_datetimes(start => $startdt, end => $enddt));
}
my $bstartdt = $format->parse_datetime("2013-02-04T00:00:00Z");
my $benddt = $format->parse_datetime("2013-02-11T00:00:00Z");
my $boundary = DateTime::Span->from_datetimes(start => $bstartdt, end => $benddt);
my $result;
print "Input:\n";
printSpanset($spanset);
# $result = invertSpansetByBoundary($spanset, $boundary);
# print "\nBaseline Result:\n";
# printSpanset($result);
$result = alternateInvert($spanset, $boundary);
print "\nImproved Result:\n";
printSpanset($result);