(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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |