Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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



  • 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 perusing the Monastery: (5)
As of 2024-04-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found