[kw-pm] Date difference

Cees Hek ceeshek at gmail.com
Mon Feb 4 14:59:10 PST 2008


On Feb 5, 2008 6:12 AM, Robert Pike <roberthpike at yahoo.com> wrote:
> I was trying to use timelocal but ran into a few
> issues (from examples and discussions I got off the
> perl monks site). Can anyone suggest a good module or
> procedure to find the number of days from Jan 1, 1968
> to a specified date? The database we use stores dates
> as an internal integer (represents number of days
> since 01/01/68) and I need to store that number as
> opposed to a more user-friendly date string. Thanks up
> front for any feedback.

Hi Robert,

The definitive date module would be DateTime, and the following gets
you your answer.

use DateTime;

my $base = DateTime->new(year => 1968, month => 1, day => 1 );
my $date = DateTime->new( year => $year, month => $month, day => $day );

my $days = $date->delta_days($base)->in_units('days');


However, the older but still useful Date::Calc module has a method for
you that makes it look much easier:

use Date::Calc qw(Delta_Days);

my $days = Delta_Days( 1968, 1, 1, $year, $month, $day);

Although I haven't benchmarked it, my guess would be that Date::Calc
will also be much faster in this case.

Cheers,

Cees Hek


More information about the kw-pm mailing list