(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"; }

In reply to Within A Date Time Range by mumbles

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.