3

鉴于以下DateTime::SpanSet包含 6 个DateTime::Span对象(打印startend 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::Spanfrom限制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]

我可以通过以下方式实现这一点:

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;
}

是否有可能以更好的方式做到这一点?

4

1 回答 1

0

The solution to this seemed too easy to I actually built a test harness to validate your results against my results, which I've included below. My Perl's a little rusty but here's the solution I came up with:

sub alternateInvert {
    my $spanset = shift;
    my $boundary = shift;
    return $boundary->complement($spanset);
}

Using this gave me the same results as your function. Here's the test harness:

#!/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);
于 2013-10-25T21:22:02.540 回答