AcidHawk has asked for the wisdom of the Perl Monks concerning the following question:
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:<!--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>
#--------------------------------------------------------------------- +------------------ # 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;
This I can get (I think) by getting (localtime)[6] and checking<!--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>
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).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; } }
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Check current time between range of dates and times
by wjw (Priest) on May 13, 2009 at 14:02 UTC | |
by AcidHawk (Vicar) on May 13, 2009 at 15:16 UTC | |
by wjw (Priest) on May 13, 2009 at 16:28 UTC | |
Re: Check current time between range of dates and times
by Marshall (Canon) on May 13, 2009 at 23:07 UTC |