go ahead... be a heretic PerlMonks

### Time::Period overlaps?

by merzy (Scribe)
 on Mar 14, 2006 at 21:04 UTC Need Help??

merzy has asked for the wisdom of the Perl Monks concerning the following question:

I love Time::Period and use it all over the place in various perverted ways. One thing has always bugged me about it, though:

I can't find an efficient way to calculate overlaps of a period and a time range.

For example, if I have a period "wd {mo-fr} hr {09-05}" that could be named the SLA and a time range (1124424000 to 1140807162) we'll, hypothetically, call the Outage.

How do I determine how many seconds of the Outage fall during the SLA period? I've come up with some hackish ways over the years, but they all come down to stepping through the entire time range with various step sizes and doing an inPeriod. Really slow. Is there a better way?

Replies are listed 'Best First'.
Re: Time::Period overlaps?
by ikegami (Patriarch) on Mar 15, 2006 at 01:20 UTC

If you already have the periods in Time::Period's format, it should be trivial to write a version of Time::Period's functions which returns DateTime::SpanSets instead of booleans. For example, below are the guts of the wd and hr.

```sub wd {
my (\$min, \$max) = @_

return DateTime::SpanSet->from_set_and_duration(
set => DateTime::Set->from_recurrence(
recurrence => sub {
my (\$dt) = @_;
return \$dt->truncate(to => 'day')
->add(days => (\$min-\$dt->wday()+7-1) % 7 + 1);
},
),
days => \$max-\$min+1,
);
}

sub hr {
my (\$min, \$max) = @_

return DateTime::SpanSet->from_set_and_duration(
set => DateTime::Set->from_recurrence(
recurrence => sub {
my (\$dt) = @_;

if \$dt->hour() >= \$min;

return \$dt->truncate(to => 'day')->set(hour => \$min);
},
),
hours => \$max-\$min+1,
)
}

# M-F, 9am to 5pm
# wd {mo-fr} hr {9-16}
my \$sla_period = wd(1,5)->intersection(hr(9, 16));

Still untested.

This looks like it would do the trick, ikegami. Thanks!
Re: Time::Period overlaps?
by ikegami (Patriarch) on Mar 15, 2006 at 00:42 UTC
Untested — I didn't install the modules &mdash but the following should do the trick:
```sub next_workday_9am {
my (\$dt) = @_;

if \$dt->hour() >= 9;

my \$wday = \$dt->wday();
if    (\$wday == 6) { \$dt = \$dt->add(days => 2); }
elsif (\$wday == 7) { \$dt = \$dt->add(days => 1); }

\$dt = \$dt->truncate(to => 'day')->set(hour => 9);

return \$dt;
}

my \$sla_period = DateTime::SpanSet->from_set_and_duration(
set => DateTime::Set->from_recurrence(
recurrence => \&next_workday_9am,
),
hours => 8,
);
);

my \$outage_start = 1124424000;
my \$outage_end   = 1140807162;

my \$outage = DateTime::Span->from_datetimes(
start  => DateTime->from_epoch(\$outage_start),
before => DateTime->from_epoch(\$outage_end),
);

print(\$sla_period->intersection(\$outage)->duration(), "\n");

DateTime::Set and related modules are definitely the way to go, although they are very daunting at first.

Re: Time::Period overlaps?
by ikegami (Patriarch) on Mar 14, 2006 at 23:25 UTC
Using Time::Period? No. I just studied the source code, and it definitely cannot do what you want (without using steping through Outage, which would constantly parse SLA). Not even close.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://536698]
Approved by spiritway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-01-27 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2022, my preferred method to securely store passwords is:

Results (70 votes). Check out past polls.

Notices?