in reply to Last day of the month. Any shorter

I think you're taking a risk by trying to implement your own leap-year calculation.
use Time::Local; sub is_last_day_of_month { my($y,$m,$d) = $_[0] =~ /(.{2,4})(..)(..)/; (gmtime(timegm(0,0,12,$d,$m-1,$y)+24*60*60))[4] != $m-1 } my $a = shift; print is_last_day_of_month($a) ? "$a: is last day of month\n" : "$a: is NOT last day of month\n";

Replies are listed 'Best First'.
Re: Re: Last day of the month. Any shorter
by greenFox (Vicar) on May 20, 2004 at 03:34 UTC

    "I think you're taking a risk by trying to implement your own leap-year calculation."

    Why? It's a simple and well documented algorithm, you can write it in a line of Perl. What's the risk?

    Personally I keep hoping that a rudimentary date time module (maybe DateTime.pm or a subset) will be included with Perl core, then using the "comes with" will be easier than cutting and pasting the line of perl around :)

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

      Still not sure why you'd want to. Just doing the leap-year check by hand is longer than some of the entire solutions presented.

      And also:

      use Time::Local; sub is_leapyear { 1 == (gmtime(timegm(0,0,1,28,1,$_[0])+24*60*60))[4] }
      (But this has severe limitations on the range of valid dates input.)

        I thought you were indicating there was a specific risk in implementing the usual algorithm and I wanted to know what it was.

        The gmtime-timegm method you give is a neat trick but I can't see any good reason to prefer it. It really needs code to catch years outside the epoch range, or at a minimum a comment to highlight it's limitation.

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

Re: Re: Last day of the month. Any shorter
by Scarborough (Hermit) on May 19, 2004 at 13:45 UTC
    The code thats gone into production uses something very similer to your example here. I'm only a contractor here and the full time staff(at the moment not perl experts) have to be able to maintain the code after I've gone, so the shortest version in this case is not the best. I just wonted to give myself a bit of a test and see where my skills where compared to the rest of the Monks. Still pretty low I'm afriad.