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

I am having a problem with my code changing to the correct month and day. When it is the end of the month the program does not go to the beginning of the next. For example, instead of changing from May 31 to June 1, it will change to June 30. Am I missing somnething? This is the code I have:
if ($start_month == "04"||"06"||"09"||"11"){ if ($start_day == 30){ $start_day = sprintf "%0.2d", $start_day+ 1; $start_day == 01; } else {$start_day = sprintf "%0.2d", $start_day + 1}; } elsif ($start_month == "01"||"03"||"05"||"07"||"08"||"10"||"12"){ if ($start_month == 12 && $start_day == 31){ $start_year ++; $start_month == 01; $start_day == 01; } elsif($start_day == 31){ $start_day = sprintf "%0.2d", $start_day+ 1; $start_day == 01; } else {$start_day = sprintf "%0.2d", $start_day+ 1}; } else { if ($start_day == 28){ $start_month == 03; $start_day == 01; } else {$start_day = sprintf "%0.2d", $start_day+ 1}; }

Replies are listed 'Best First'.
Re: Changing dates
by moritz (Cardinal) on Dec 17, 2008 at 18:52 UTC
    Whenever you do date calculations manually, you are making a big mistake. They are harder than you think, and you never get them quite right.

    Thankfully there are many well tested modules on CPAN for that, for example DateTime, Date::Manip, Date::Calc.

    If you just want to deal with days, and never with times, Date::Simple can really be recommended, it uses a super-easy interfaces probably does exactly what you want (whatever it is) very easily.

Re: Changing dates
by toolic (Bishop) on Dec 17, 2008 at 18:49 UTC
    Instead of:
    if ($start_month == "04"||"06"||"09"||"11"){

    You probably really want:

    if ($start_month eq "04" || $start_month eq "06" || $start_month eq "0 +9" || $start_month eq "11"){

    or even (using grep and "eq" for string comparison):

    if (grep {$start_month eq $_ } qw(04 06 09 11)) {

    or a hash.

    But you probably really should use a CPAN module for this.

Re: Changing dates
by johngg (Canon) on Dec 17, 2008 at 18:56 UTC

    You may find this recent post of interest.

    Cheers,

    JohnGG

Re: Changing dates
by MidLifeXis (Monsignor) on Dec 17, 2008 at 18:54 UTC

    Did you run this through use strict; use warnings;? Check the precedence of your operators. This code:

    if ($foo == "A"||"B"||"C")

    is actually the same as

    if (($foo == "A") || ("B") || ("C"))

    which should reduce to if (1). Also, you have a $start_day == 01 in every section. == is a comparison, = is an assignment.

    --MidLifeXis

      Did you run this through use strict; use warnings;?

      How would either help in this case? I don't get any errors or warnings about this construct with Perl 5.8.8 or 5.10.0.

        D'oh. Must be the ass-u-me thing. Thought that the $var == 01 at the end of the if blocks would trigger a "useless" something or another. My mistake.

        --MidLifeXis

      Also, you have a $start_day == 01 in every section. == is a comparison, = is an assignment.

      thanks for pointing that out.....problem solved
Re: Changing dates
by DStaal (Chaplain) on Dec 17, 2008 at 18:54 UTC
Re: Changing dates
by imrags (Monk) on Dec 18, 2008 at 06:42 UTC
    I've used Date::Pcalc to modify dates and I really like it...
    It is very useful to add/subtract/calculate dates...
    Raghu