http://qs1969.pair.com?node_id=303327


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