listmail wrote:
> In the code below I started testing my options of allowing for a 
> maintenance window for specific days/hours to be specified and then 
> attempted to calculate if a specific day/time fell within that defined 
> window.  I'm open to suggestions and would like to know if there is a 
> better approach altogether for something like this.
...
> #Start: Sunday 2330 (11:30pm) and Stop: Tuesday's 0930 (9:30am)
> my $timespec="6:2330-2:0930";

What is your start/end time format ?  6: would be saturday (sunday is day 0).

How long can the window be ?  Greater than a week ?  Can the window be last
week or next week as well as this week ?  If so, you'll need more than the
DOW for a start/end day.

Ultimately what you want to do is convert the two times to epoch time and
see if the current time is between those two times.

Once you straighten out your input format and convert to epoch it's just
simple code (I used a YYYYMMDD HHMM format for my code).  If you can't cross
year boundaries, than you could use the current year and leave the year off.

use strict;
use warnings;
use Time::Local;

# ARGV[0] = start-time (YYYYMMDD HHMM)
# ARGV[1] = end-time (YYYYMMDD HHMM)

# You can add some delimiters and use a single string if you like -
# just a little more work.  If you want to use the DOW vs YYYYMMDD like
# you were trying to do, then you'll need to answer the above questions.

my $bd = shift || '20070610 2330';      # begin date - Sun 2330
my $ed = shift || '20070613 0930';      # end date - Wed 0930

my $ce = time;                  # current local epoch time
my $be = cvt_date ($bd);        # maint window begin epoch time
my $ee = cvt_date ($ed);        # maint window end epoch time
# printf "be=%u, ce=%u, ee=%u\n", $be, $ce, $ee;

if ($ce >= $be and $ce <= $ee) {
        print "In maint window\n";
} else {
        print "Not in maint window\n";
}
exit;

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub cvt_date {  # @cd = cvt_date ('YYYYMMDD HHMM');
        my $date = shift;

my @cd;
$cd[7] = 0;
$cd[6] = 0;
$cd[5] = substr ($date, 0, 4) - 1900;
$cd[4] = substr ($date, 4, 2) - 1;
$cd[3] = substr $date, 6, 2;
$cd[2] = substr $date, 9, 2;
$cd[1] = substr $date, 11, 2;
$cd[0] = 0;
return timelocal @cd;

}

__END__
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to