When I see a certain condition (I.e. a file is written in a directory) I need to perform an action. I am wanting to check that the current time (I.e. now) is between certain dates and times and if so I will NOT perform this action. So this is a simple exclusion\blackout list and I identify which config element to use based on criteria in the file that I have read from the dir. The configuration is in xml and looks as follows
<!--MyExample_1 will not perform any action between 1st January 2006 1 +2 noon and 2nd March 2006 12 noon--> <resource name="MYEXAMPLE_1" start="01-01-2006 12:00" end="02-03-2006 +12:00"></resource> <!--MyExample_1b does NOT have an end time defined so from the Start t +ime for evermore NO actions will be performed--> <resource name="MYEXAMPLE_1B" start="01-01-2006 12:00" end=""></resour +ce> <!--MyExample_2 will not perform any actions between 7 PM and 9 PM eve +ry day. every stands for all days (IE MON - SUN)--> <resource name="MYEXAMPLE_2" start="every 19:00" end="every 21:00"></r +esource> <!--MyExample_3 will never perform ANY actions --> <resource name="MYEXAMPLE_3" start="" end=""></resource>
What I do is convert the start time to epoch and then the end time to epoch and check if the current epoch is between these two times. I have a method that looks as follows:
#--------------------------------------------------------------------- +------------------ # Convert supplied date to Epoch time #--------------------------------------------------------------------- +------------------ sub toEpoch { my $date = shift; my ($epoch, @suppliedTime, @suppliedDate); my @tempdate = split/\s/, $date; @suppliedTime = split/:/, $tempdate[1]; if (uc($tempdate[0]) eq "EVERY") { #print "\t\t**This will return the epoch to this time TODAY**\ +n"; $epoch = timelocal(0, $suppliedTime[1], $suppliedTime[0], (loc +altime)[3], (localtime)[4], ((localtime)[5]+1900)); } else { @suppliedDate = split/-/, $tempdate[0]; $epoch = timelocal(0, $suppliedTime[1], $suppliedTime[0], $sup +pliedDate[0], ($suppliedDate[1] - 1), $suppliedDate[2]); } return($epoch); }

Obviously, if there is no end time, or no start and no end time, I handle that accordingly (see the config xml comments). You will also see in the toEpoch function if the word "every" is received as the first element in the configured date that is passed from the configuration I check only the times i.e. start time and end time for each and every day.

So far this rudimentary way of handling the checking if the current time falls between two configurable exclusion\blackout times has worked well. However, some-one has now asked that we exclude/blackout processing the actions every Monday. I now have to come up with a way of identifying if it is Monday between the times in the config and if say it is Tuesday perform the action. however if it is any Monday between the defined times do not perform the action. I would like the configuration to look as follows;

<!--MyExample_2 will not perform any actions between 7 PM and 9 PM eve +ry Monday.--> <resource name="MYEXAMPLE_2" start="mon 19:00" end="mon 21:00"></resou +rce>
This I can get (I think) by getting (localtime)[6] and checking
my $configuredDayFromXML = "mon"; #obtain from configuration my @daysOfWeek = ('Sun', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'); if ($configuredDayFromXML eq $daysOfWeek[(localtime)[6]]) { if (! check if we are between the start and end time) { perform action; } }
The problem I am trying to rack my head around is, if I want to configure a blackout period of say between "Mon 11:00" and "Wed 11:30" how would I identify the epoch time and how would I check between say "Fri 16:00" and "Mon 8:00" (I.e. nothing over the weekend).

I hope this explanation has not proved to be too tiresome. Regards AcidHawk

-----
Of all the things I've lost in my life, its my mind I miss the most.

In reply to Check current time between range of dates and times by AcidHawk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.