Hi,
This is the code i originally had and this is what i replaced with. both are 
hogging a lot of time.

sub calcmonth_old
{
     my $string1 = shift;
     my $string2=shift;
     my $date1 = ParseDate($string2);
     my $date2 = ParseDate($string1);
     my $err = "";
     my $delta = DateCalc($date1,$date2,\$err,1);
     # YY:MM:WK:DD:HH:MM:SS  the years, months, etc. between the two
     $delta=~ s/\+|\-//;
     my ($year,$month,$wk,$dd,$hh,$mm,$ss)= split(/:/,$delta);
     $month = ($year * 12)+$month;
     return $month;
}

sub calcmonth
{
          my $string1=shift;
     my $string2=shift;
         if(trim($string1) eq "" or  trim($string2) eq "")
     {
          return "";
     }

         my $string1 = str_pad($string1,8,"01",1);
         my $string2 = str_pad($string2,8,"01",1);
     my $err = "";
        if($string1 < $string2)
        {
                ($string1,$string2)=($string2,$string1);
        }
         my $year1 = substr($string1,0,4);
         my $year2 = substr($string2,0,4);
         my $month1= substr($string1,4,2);
         my $month2= substr($string2,4,2);
         my $day1 = substr($string1,6,2);
         my $day2 = substr($string2,6,2);
                my $monthDiff =0;
        while($string2 < $string1)
        {
                my ($year,$month,$day) =
      Add_Delta_YM($year2,$month2,$day2,
                   0,1);
                $string2 = $year.str_pad($month,2,"0",2).str_pad($day,2,"0",2);
                if($string2 <= $string1)
                {
                        $monthDiff++;
                        $year2 = substr($string2,0,4);
                        $month2= substr($string2,4,2);
                        $day2 = substr($string2,6,2);
                }
        }
        return $monthDiff;

}

use constant STR_PAD_RIGHT => 1;
use constant STR_PAD_LEFT  => 2;
use constant STR_PAD_BOTH  => 3;
 
sub str_pad
{
    my $input =shift;
    my $length = shift;
    my $pad = shift;
    my $options  =  shift;
 
    return $input if $length < length $input;
 
    # Work out where to place our string.
    my $start = 0;
    my $diff = $length - length $input;
    my $rv;
 if($options==undef)
 {
  $options=STR_PAD_RIGHT;#default argument is Right pad
 }
 if($pad eq undef)
 {
  $pad=" ";#default pad is space
 }
    if ( $options == STR_PAD_RIGHT )
    {
        my $padding = substr( $pad x $diff, 0, $diff );
        $rv = $input . $padding;
    }
    elsif ( $options == STR_PAD_LEFT )
    {
        my $padding = substr( $pad x $diff, 0, $diff );
        $rv = $padding . $input;
 
    }
    elsif ($options == STR_PAD_BOTH )
    {
        $rv = substr( $pad x $length, 0, $length );
        substr( $rv, $diff / 2, length $input ) = $input;
    }
    else
    {
        print "Invalid 4th argument to str_pad";
    }
 
    return $rv;
}

sub trim 
{
  my $a = $_[0];
  $a=~s/^\s+//g;
  $a=~s/\s+$//g;
  $a='' if (!defined($_[0]));
   return $a;
}

Thanks,
Badri.

-----Original Message-----
From: Hill, Ronald [mailto:[EMAIL PROTECTED]
Sent: Tuesday, October 18, 2005 2:51 PM
To: Soundararajan, Badrinarayanan
Cc: datetime@perl.org
Subject: RE: months between 2 dates


Hello Badri,


Soundararajan, Badrinarayanan wrote:
> Hi,
> 
(snipped)
> 
> Initially i used DateManip's DateCalc method, but that is hogging too
> much memory. 

Perhaps you could provide the code you have. 

> So i am trying to move to DateCalc.
> 
> Could you give me the basic logic and conditions to check for months
> between 2 dates 
> 

The RECIPES section of that module has something you could use?
How do I check whether a given date lies within a certain range of
dates?

  use Date::Calc qw( Date_to_Days );

  $lower = Date_to_Days($year1,$month1,$day1);
  $upper = Date_to_Days($year2,$month2,$day2);

  $date = Date_to_Days($year,$month,$day);

  if (($date >= $lower) && ($date <= $upper))
  {
      # ok
  }
  else
  {
      # not ok
  }

I hope this helps.

Ron


"Secure Server" made the following
 annotations on 10/18/2005 02:53:32 PM
------------------------------"This e-mail, including attachments, may include 
confidential and/or proprietary information, and may be used only by the person 
or entity to which it is addressed. If the reader of this e-mail is not the 
intended recipient or his or her authorized agent, the reader is hereby 
notified that any dissemination, distribution or copying of this e-mail is 
prohibited. If you have received this e-mail in error, please notify the sender 
by replying to this message and delete this e-mail immediately."
==============================

Reply via email to