in reply to Need Advice: Date difference revisted

Ok... Here's an ugly hack that works in Perl 4. I don't know what if it's sufficient for your needs but I got curious and hacked away. It will fail in the year 2100, but you can work in the additional logic to handle the leap year exceptions.... God hopefully, this kludge won't be around in 96 years. Anyway, the main part is the function I called "diffTime" that returns the number of seconds since the supplied date and time. It returns a negative value for past dates and positive values for future dates. It only works with dates after 1970 and times must be in 24 hour format. Curious to know if this is at all useful to you.

Synopsis: &diffTime(3/10/2004 02:28:00)

-Aaron Greenberg
ahg at pobox dot com

#!/usr/local/bin/perl4 # Global config option for hours off of GMT. i.e. EST is -5. # NOTE: You may want to use something like 4.5 to average when Dayligh +t # savings time is in affect. $GMToffset = -5; # We Run from the commandline as sample program $diff = &diffTime($ARGV[0], $ARGV[1]); print "Diff Seconds: $diff\n"; $diffDays = $diff/(3600*24); print "Diff Days: $diffDays\n"; if ($diffDays < -16) { ### NOTE that it's negative for past days. print "\n*** Windows up for more than 16 days!\n"; } exit(0); # FUNCTION: diffTime (date time) # Usage &difftime( DD/MM/YYYY [HH:MM:SS] ) # A hyphen may be used as the date separator. only colons valid for ti +me # Returns seconds from current time. Negative for past events Posotiv +e for # future events. sub diffTime { local($rebootDate, $rebootTime, $rDmon, $rDday, $rDyear, $rDhour, $ +rDmin, $rDsec); local(@monthdays); local($sec, $leapCount, $i, $time); $rebootDate = $_[0]; # First Argument $rebootTime = $_[1]; # Second Argument unless (($rDmon, $rDday, $rDyear) = $rebootDate =~ /(\d{1,2})[\/-]{ +1}(\d{1,2})[\/-]{1}(\d{4})/) { print STDERR "Invalid Date\n\n"; exit(1); } @monthdays=(31,28,31,30,31,30,31,31,30,31,30,31); ### NOTE: This will break in the year 2100 if ($rDyear%4 == 0) { $monthdays[2]=29; } $sec = ($rDyear - 1970 ) * 365 * 24 * 60 * 60; $leapCount = int(($rDyear - 1972) / 4); $sec = $sec + ($leapCount * 24 * 60 * 60); for ( $i = $rDmon-2; $i >= 0; $i--) { $sec = $sec + ( $monthdays[$i] * 24 * 60 * 60); } $sec = $sec + ($rDday * 24 * 60 * 60); if ($rebootTime) { unless (($rDhour, $rDmin, $rDsec) = $rebootTime =~ /(\d{1,2})[:] +{1}(\d{1,2})[:]{1}(\d{1,2})/) { print STDERR "Invalid Time\n\n"; exit(2); } $sec = $sec + ($rDhour * 3600) + ($rDmin * 60) + $rDsec; } unless ($GMToffset) { $GMToffset = 0; } $time = time() + $GMToffset * 60 * 60; return $sec - $time; }