Calculate days between yyyyMmmdd dates on Solaris

I extract dates from the log file and need to calculate days between two dates. My dates are in yyyyMmmdd format. Example:

$d1=2011 Oct 21
$d2=2012 Feb 20

I need to calculate the number of days between $d2 and $d1. This is on Solaris.
Any ideas?

Thanks,
djanu

Please go into the FAQ, look for date arithmetic by a user named Perderabo. The datecalc script there will do almost any date operation.

Wow. This is quite a script. All I need is number of days. There are no easier ways?

---------- Post updated 02-24-12 at 10:17 AM ---------- Previous update was 02-23-12 at 05:01 PM ----------

Any ideas how to calculate number of days between 2011 Oct 21 and 2012 Feb 20 ?

#! /usr/bin/perl
use warnings;
use strict;
use Time::Local 'timelocal';

my ($d1, $d2, %mnths, @x, @y, $s1, $s2);

($d1, $d2) = ("2012 Jan 31", "2012 Mar 01");

%mnths = ( "Jan" => "00", "Feb" => "01", "Mar" => "02", "Apr" => "03", "May" => "04", "Jun" => "05",
           "Jul" => "06", "Aug" => "07", "Sep" => "08", "Oct" => "09", "Nov" => "10", "Dec" => "11" );

@x = split /\s+/, $d1;
@y = split /\s+/, $d2;

$s1 = timelocal (0, 0, 0, $x[2], $mnths{$x[1]}, $x[0]);
$s2 = timelocal (0, 0, 0, $y[2], $mnths{$y[1]}, $y[0]);

print "No. of days: ", (($s2 - $s1) / (3600 * 24)) + 1; # This is inclusive of both dates. If you don't want it like this, then remove "+1" from the end.

Date math isn't trivial. Ever tried rolling your own?

Certainly there's easier ways. Try GNU date.

Oh, you don't have GNU date? Try Perl.

Oh, you can't install Perl? Maybe you can coerce MySQL or AWK into doing it...

No go? Try the big script, then.

People suggest the script because you didn't bother posting what your system was. Instead of a few rounds of questions, they give you something that either works or can be made to work almost no matter what...