Another way to do it:
---
use strict;
use DateTime::Incomplete;
use DateTime::SpanSet;

my $blackout_start =
    DateTime::Incomplete->new(
        hour   => 22,
        minute => 0,
        second => 0,
    );
my $blackout_end  =
    DateTime::Incomplete->new(
        hour   => 23,
        minute => 0,
        second => 0,
    );
my $blackout =
    DateTime::SpanSet->from_sets(
        start_set => $blackout_start->to_recurrence,
        end_set   => $blackout_end->to_recurrence,
    );

print "blackout time is from ",
    $blackout_start->datetime, "\n until ",
    $blackout_end->datetime, "\n";
test( DateTime->now );
test( DateTime->now->set( hour => 22 ) );
test( DateTime->now->set( hour => 23 ) );

sub test {
    print $_[0]->datetime;
    if ( $blackout->contains( $_[0] ) )
    {
       print " Inside Blackout...no checks !\n";
    }
    else
    {
       print " Outside Blackout...\n";
    }
}
--- output ---
blackout time is from xxxx-xx-xxT22:00:00
 until xxxx-xx-xxT23:00:00
2004-02-25T21:29:13 Outside Blackout...
2004-02-25T22:29:14 Inside Blackout...no checks !
2004-02-25T23:29:14 Outside Blackout...
---

> > Hi !
> > 
> > I have to do some checks every minute. For
maintenance I have to define a
> > blackout-period with
> > no checks (example: blackout start 22:00, end:
23:00, interval: daily).
> > 
> > I have had a look at DateTime::Span, but
unfortunately I'm a little bit
> > confused...
> > 
> > I build a "Span":
> > 
> > $iso8601 = DateTime::Format::ISO8601->new;
> > $blackout_dt_start = $iso8601->parse_datetime(
"22:00" );
> > $blackout_dt_end = $iso8601->parse_datetime(
"23:00" );
> > $span = DateTime::Span->from_datetimes( start =>
$blackout_dt_start, end =>
> > $blackout_dt_end );
> > 
> > and made the checks:
> > 
> > while (1)
> > {
> >     my $dt_now = DateTime->now;
> >     if ( $span->contains( $dt_now))
> >     {
> >        print " Inside Blackout...no checks !";
> >     }
> >     else
> >     {
> >        print " Outside Blackout...";
> >        do_check();
> >     }
> > 
> >     sleep (60);
> > }
> > 
> > My question: how to build recurring "span" for
every day? I have had a look
> > at DateTime::SpanSet but there
> > is no method "from_span_and_duration" like the
existing method
> > "from_set_and_duration"....
> > 
> > 
> > 
> > ---
> > 
> >   Oliver Raupach



Reply via email to