I am getting dates in a YYMMDD format. I need to find out if they are more than X days in the past. I want to install the Date::Calc module but it requires an ANSI C compiler. The boss says I can use anything that I can build with straight perl or nmake.exe (Windows systems), but cannot install an ANSI C compiler. Since we don't currently use it, he doesn't want it on production servers.
Are there other modules that would allow this? Here's a snippet of what I tried, but it didn't work:
use Time::Local;
use constant MAX_AGE => 20;
# Other code here
sub older_than_MAX_AGE {
my $date = _convert_date( $_[0] ); # convert yymmdd t
+o epoch seconds
my $max_epoch_seconds = MAX_AGE * 60 * 60 * 24; # MAX_AGE should b
+e days. This converts it to seconds.
my ( $day, $month, $year ) = (localtime)[3..5];
$year += 1900;
my $today = timelocal( 0,0,0, $day, $month, $year );
my $cutoff_date = $today - $max_epoch_seconds;
return $date < $cutoff_date ? 1 : 0 ;
}
sub _convert_date {
# This takes a date in YYMMDD format and converts it to epoch seco
+nds (assumes 12:00:00 midnight)
my $date = shift;
my ( $year, $month, $day ) = ( $date =~ /(\d\d)(\d\d)(\d\d)/ );
$year += 2000;
timelocal( 0,0,0,$day,$month,$year );
}
When I pass "older_than_MAX_AGE" the date of 010329, I get the following data:
$cutoff_date |
'986281200' |
$date |
'988527600' |
$max_epoch_seconds |
1728000 |
$_[0] |
'010329' |
$today |
'988009200' |
As you can see, it says that the date that I am passing in is *newer* than today: 988527600 > 988009200, even though today is 010423 and the date passed in is 010329. Can anyone see what I'm doing wrong?
Thanks, J. Atzger