One of my Perl scripts here at work used the Add_Delta_Days
subroutine from the Date::Calc module to do some calendar date arithmetic. I'm in the process of building a new machine on which this script will run, and I don't have access to an external network. Unfortunately, the install process for Date::Calc is fairly difficult. The module relies on a C library which must be compiled with the same compiler as was used to build the local Perl install. To make matters worse, the modules that Date::Calc is dependent on have similar requirements. As a result, I decided to skip installing this non-standard module, and instead use a home-brew replacement. It turns out that Add_Delta_Days
is fairly straightforward to replace:
use Time::Local; # Standard module
sub addDaysToDate
{
my ($y, $m, $d, $offset) = @_;
# Convert the incoming date to epoch seconds
my $TIME = timelocal(0, 0, 0, $d, $m-1, $y-1900);
# Convert the offset from days to seconds and add
# to our epoch seconds value
$TIME += 60 * 60 * 24 * $offset;
# Convert the epoch seconds back to a legal 'calendar date'
# and return the date pieces
my @values = localtime($TIME);
return ($values[5] + 1900, $values[4] + 1, $values[3]);
}
You call this subroutine like this:
my $year = 2009;
my $month = 4;
my $day = 22;
my ($nYear, $nMonth, $nDay) = addDaysToDate($year, $month, $day, 30);
This subroutine isn't a one-to-one replacement, obviously. Unlike Date::Calc, my home-brew subroutine suffers from the Year 2038 problem (at least on 32-bit operating systems). It likewise can't go back in time by incredible amounts (I'm bound to the deltas around the epoch). However, this workaround saves me a bunch of setup time, and works just as well.