Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Representing windows of time in a string

by Limbic~Region (Chancellor)
on Oct 30, 2003 at 17:03 UTC ( [id://303327]=note: print w/replies, xml ) Need Help??


in reply to Representing windows of time in a string

Nitrox,
As I mentioned in the CB, The Panther has a section on using bitmap vectors to determine scheduling conflicts. It is just a matter of logically AND-ing the two bitmaps.

Each hour in the week would be one bit. You would be storing a 21 byte bitmap vector. If the bit is set, then there is a problem if the event happens at this hour. The task then is to design a way to parse your range and localtime() into the bitmap vector.

The following is very basic, but it gives you a fully functional framework. Expand it as you see fit.

#!/usr/bin/perl -w use strict; my $interval = "Mon 3-4, Mon 8-12, Wed 4-7, Thu 11-18"; my $alert = interval_parse( $interval ); my $alarm = get_current(); print "Page someone\n" if Critical($alert, $alarm); sub interval_parse { my $interval = shift; my $bitmap = ""; my %day_offset = ( sun => 0, mon => 24, tue => 48, wed => 72, thu => 96, fri => 1 +20, sat => 144 ); for my $range (split /,/ , $interval) { my ($day, $from, $to) = ($range =~ /([A-Za-z]+)\s+(\d+)-(\d+)/ +); my $offset = $day_offset{lc $day}; $from += $offset; $to += $offset; vec( $bitmap, $_, 1 ) = 1 for $from .. $to; } return $bitmap; } sub get_current { my ($hour, $day) = (localtime())[2,6]; my $bitmap = ""; my $now = $day * 24 + $hour; vec ( $bitmap, $now, 1 ) = 1; return $bitmap; } sub Critical { my ($alert, $alarm) = @_; my ($combined) = $alert & $alarm; my $offset = length ($combined) * 8; while (--$offset >= 0) { return 1 if vec($combined, $offset, 1); } return 0; }

I hope this helps - L~R

Replies are listed 'Best First'.
Re: Re: Representing windows of time in a string
by Nitrox (Chaplain) on Oct 30, 2003 at 19:07 UTC
    L~R, thanks for the full framework! I was headed down the same path and manipulated the examples from the Panther to work with 24 hours per day:
    #!/usr/bin/perl -w use strict; my %base_hours = ( sun => 0, mon => 24, tue => 48, wed => 72 , thu => +96, fri => 120, sat => 144 ); my $vector = interval_parse("Sat 9-17, Sun 9-17"); my $bits = unpack("b*", $vector); print $bits, "\n"; sub interval_parse { my ($interval_sequence) = @_; my ($time_range) = ""; foreach my $day_hours (split /,/, $interval_sequence) { my ($day, $from, $to) = ($day_hours =~ /([A-Za-z]+).*(\d+)-(\d+ +)/); my $base = $base_hours{lc $day}; $from += $base; $to += $base; for (my $i = $from; $i < $to; $i++) { vec($time_range, $i, 1) = 1; } } return($time_range); }
    Thanks for the pointer!

    -Nitrox

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://303327]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found