Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Representing windows of time in a string by Limbic~Region
in thread Representing windows of time in a string by Nitrox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found