in reply to Re: Re: Calculating between times
in thread Calculating between times

Last I checked, February, being the second month, doesn't in fact have 31 days. If you expected that to be March, then you have an off by one error and you should add one to the month column before attempting to format it.

On the other, if you expect february to have 31 days, well, you have more problems then these perl ones!

Replies are listed 'Best First'.
Re: Re: Re: Re: Calculating between times
by greenFox (Vicar) on Jan 09, 2004 at 06:33 UTC

    Whilst a 30 day February is not unheard of I agree, 31 is stretching it a bit too far :-)

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: Re: Re: Re: Calculating between times
by Anonymous Monk on Jan 09, 2004 at 06:43 UTC
    The thing is, I'm not testing the dates as they put them in. I have three pull down menus (day, month, year) where they select the according numbers. They have a 31 day limit and I don't do ANY checking on whether or not the month days actually exist.

    Unless you know of a perl snippet that could test to be sure these dates are valid, I'm not sure what to do.

      The thing is, I'm not testing the dates as they put them in. I have three pull down menus (day, month, year) where they select the according numbers. They have a 31 day limit and I don't do ANY checking on whether or not the month days actually exist. Unless you know of a perl snippet that could test to be sure these dates are valid, I'm not sure what to do.
      eval{ format($date); }; if ($@ =~ /range/) { print "Range error!"; }
      If you can afford using a module, I recommend Date::Calc. It has a check_date function that can check if any date is valid, and also a Days_in_Month function that returns the number of days in a given month.

      I also recommend Date::Calc for any date/time calculations. IMHO, date/time calculations is one of those things that has been done too many times, but is always a source of errors when you try to do it yourself... From converting seconds to hours, to adding days to a date while taking into account the leap year calculations and everything, this module can simplify error-prone date/time calculations greatly.

      (Geez, that sounds like a sales pitch... I'm serious about those types of calculations being error prone though. Just go through Date::Calc's POD, you'll see how many things the author took into account in the module)