vennirajan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a problem with the DateTime::Precise module. I have created the object with the following parameters.

  • Year -> 2006
  • Month -> 01 (Jan)
  • Date -> 31

  • The problem i am facing here is, when i increment the month by 1, it increments the date by 31 days and so it gets the date 2/31. But which is not a valid date. So, perl compensates by converting the days over into the next month. So 2/31 becomes 3/3.

    I have put this piece of code in another script which processes the records from the text file. I am facing this problem only when create the object from the January month having the date > 29.

    Can any one tell me how to get the correct month value when i do inc_month from the January object (having the $date >29 )?

    I can put a if condition to check the january objects.But, I am still trying to solve it through the module's methods.

    Have any of you faced this problem ? Any help would be highly appreciated.

    Thanks for your valuable time.

    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                     BK Systems.

    • Comment on How to get the correct incremented month through DateTime::Precise ?

    Replies are listed 'Best First'.
    Re: How to get the correct incremented month through DateTime::Precise ?
    by davorg (Chancellor) on Oct 16, 2006 at 10:18 UTC

      Can you show us the code that you are using to add one month? There are a number of different ways to add durations to a datetime object. You may find the section How Datetime Maths is done from the DateTime documentation will be useful.

      Can any one tell me how to get the correct month value

      Perhaps that would be easier to do if you told us what you consider the "correct month value" to be. A month isn't a precise duration. It's hard to add one accurately.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

    Re: How to get the correct incremented month through DateTime::Precise ?
    by ashokpj (Hermit) on Oct 16, 2006 at 11:18 UTC
      #/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Piece::Month; use Time::Seconds; my $date = Time::Piece->strptime( "2002-01-31", '%Y-%m-%d' ); $date += ONE_MONTH; print "\n Date:",$date->ymd;

        That's a rather strange approach to take. Time::Seconds defines one month as 2,629,744 seconds (exactly one twelfth of a year). So by adding one month, you're actually adding 30 days, 10 hours, 29 minutes and 4 seconds. Which seems a strange way to approach this problem.

        This issue becomes obvious if you print out the time parts of your object too.

        #/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Seconds; my $date = Time::Piece->strptime( "2002-01-31", '%Y-%m-%d' ); print "\n Date:",$date->ymd, ' ', $date->hms; $date += ONE_MONTH; print "\n Date:",$date->ymd, ' ', $date->hms;

        Which displays:

        Date:2002-01-31 00:00:00 Date:2002-03-02 10:29:04
        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg