Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Within A Date Time Range

by mumbles (Friar)
on Jul 07, 2001 at 07:25 UTC ( #94680=perlquestion: print w/ replies, xml ) Need Help??
mumbles has asked for the wisdom of the Perl Monks concerning the following question:

(Somehow I feel this has been done before but I was unable to find it)

All I want to accomplish on an NT system is given a starting date and time and an ending date and time check if the current date and time fall within the range.

Well, it sounded simple enough when I started the adventure but soon spun into a monster that is beyond my level and now I feel I must ask for some assistance.

I think the logic is working, but it looks so different than the succinct code I see here on a daily basis. The exit() statements, among other things, seems not very perlish. Somehow it must be possible to gracefully construct this depending on a nice if/else statement or, as I fear, one ingenious line.

There has got to be a better way.

When I began this snippet from what will be my first real script (beyond "hello world") I was even looking up how to print so please take that in to consideration. By now I know very well how to print and rarely forget a terminating ";" although all those different brackets still leave me scratching my head :)

Please, all hints, suggestions, and the harshest criticism taken with the greatest appreciation. Lead me to the light...

use Date::Calc qw( Today Date_to_Days Now ); # Lower Limit my $year1 = 2001; my $month1 = 7; my $day1 = 6; my $hour1 = 20; my $min1 = 00; my $minlower = ($hour1 * 60) + $min1; # Upper Limit my $year2 = 2001; my $month2 = 7; my $day2 = 6; my $hour2 = 20; my $min2 = 59; my $minupper = ($hour2 * 60) + $min2; # Current System Time my $now = localtime(); print $now, "\n"; # Get current time from module ($year,$month,$day) = Today(); ($hour,$min,) = Now(); my $minnow = ($hour * 60) + $min; print "It is now $hour:$min or $minnow minutes since midnight\n"; $lower = Date_to_Days($year1,$month1,$day1); $upper = Date_to_Days($year2,$month2,$day2); $date = Date_to_Days($year, $month, $day); print "$lower=lower\t$upper=upper\t$date=date\n"; if (($date >= $lower) && ($date <= $upper)) { if (($date != $lower) && ($date != $upper)) { print "Not on a start and stop day\n"; }elsif (($date == $lower) && ($date == $upper)) { print "same start and stop date\n"; if (($minnow >= $minlower) && ($minnow < $minupper)) { print "match on dates and mins within range\n"; }else{ print "BUT not within minute range\n"; exit(); } }elsif (($date == $lower) && ($minnow < $minlower)) { print "before start time\n"; exit(); }elsif (($date == $upper) && ($minnow > $minupper)) { print "after end time\n"; exit(); } print "GOOD\n"; }else{ print "out of range\n"; }

Comment on Within A Date Time Range
Download Code
Re: Within A Date Time Range
by voyager (Friar) on Jul 07, 2001 at 07:52 UTC
    From the "Perl Cookbook", recipe 3.2: Convert your dates to epoch seconds and see if current time is between them:
    use Time::Local; my $time1 = timelocal($sec1, $min1, $hours1, $mday1, $mon1, $year1); my $time2 = timelocal($sec2, $min2, $hours2, $mday2, $mon2, $year2); my $now = localtime; if ( $now > $time1 && $now < $time2 ) { # do stuff if within range } else { # do stuff if not in range }
Re: Within A Date Time Range
by andreychek (Parson) on Jul 07, 2001 at 07:55 UTC
    I've always found it easiest to deal with time/date stuff by first converting the dates I'm dealing with to seconds since the epoch. Which, conviently, is returned to us by the time function. Additionally, we can make use of the Time::Local module to convert dates to seconds. So, I might try something like this:
    use strict; use Time::Local; # Time::Local uses an index of 0 for the month # (as does localtime and gmtime) # Lower Limit my $year1 = 2001; my $month1 = 7; my $day1 = 6; my $hour1 = 20; my $min1 = 00; my $sec1 = 00; # Upper Limit my $year2 = 2001; my $month2 = 7; my $day2 = 6; my $hour2 = 20; my $min2 = 59; my $sec2 = 00; # Convert the lower limit into seconds my $min_time = timelocal($sec1,$min1,$hour1,$day1,$month1,$year1); # Convert the upper limit into seconds my $max_time = timelocal($sec2,$min2,$hour2,$day2,$month2,$year2); # Get the current time, in seconds my $now = time(); # If we haven't yet reached the minimum time if ($now < $min_time) { print "It's before my time!"; } # If we're past due elsif ($now > $max_time) { print "It's after my time!"; } # Otherwise, we're right on time! else { print "Ahhh, just right!"; }
    Now, I'm not quite sure what you're using this for -- but do remember, if you just need tasks to be run every so often, you always have the NT "at" scheduler you can use to schedule jobs to run on a periodic basis.
Re: Within A Date Time Range
by mumbles (Friar) on Jul 07, 2001 at 16:10 UTC

    Thanks voyager and andreychek,

    But it breaks :(

    This is where I started off before devolving into that nasty nested "if" business. Running that with a start date of today and a start time that's past and a ending date of today and a time that's yet to come results in a "It's before my time!".


    It's now july 7 2001 and time is 7:43. With lower end of range set for midnight today and the upper end of range is set for noon today.

    Variable results are:

    $min_time = 997160400

    $max_time = 997200000

    $now = 994506189

    Or am I missing something?

      Check the documentation for Time::Local, in particular what the ranges are for year and month. From the doc:
      It is worth drawing particular attention to the expected ranges for the values provided. While the day of the month is expected to be in the range 1..31, the month should be in the range 0..11. This is consistent with the values returned from localtime() and gmtime().
      Note range for month is 0..11, though days are 1..31! Also, the allowed ranges for year are flexible, though not necessarily what you'd expect.


        So all that was required was to subtract 1 from $month1 and $month2 and all is ok?! I read the module and didn't get it. I read the section in the cookbook and still didn't get it. I was even told the month range was 0..11 and I still didn't get a clue.

        I don't want to tell you how many hours I spent on this.

        Thank you very much.

        For pennance this newbie is going to whack himself over the head with all the surrounding Perl books

        BTW, this will be called from another program that pings servers and gateways every five minutes. Should a machine not respond it will check with this script to make sure that the downtime isn't scheduled before sending pages/emails etc.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://94680]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (17)
As of 2014-08-27 17:12 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (247 votes), past polls