develooper Front page | perl.fwp | Postings from May 2003

are we having fun yet?

Thread Next
From:
Stewart C . Russell
Date:
May 23, 2003 11:54
Subject:
are we having fun yet?
Message ID:
20030523185432.NXCC19248.tomts13-srv.bellnexxia.net@[209.226.175.20]
John W. Krahn wrote:
> 
> I guess some people don't know how to have fun.  
> :-(

could it be fun to come up with the most Perlish way to roll-your-own days-in-the-month routine? Using someone else's module is probably ideal.

Here's my baroque and probably wrong first try:

sub days_in_month($$) {    # month, year
    my ( $month, $year ) = @_;
    return unless (defined($month) and defined($year));
    return if ( $year < 0 );
    return if (( $month < 1 ) or ( $month > 12 ));
    my %days = (
        1  => 31,          # january
        2  => undef,       # february
        3  => 31,          # march
        4  => 30,          # april
        5  => 31,          # may
        6  => 30,          # june
        7  => 31,          # july
        8  => 31,          # august
        9  => 30,          # september
        10 => 31,          # october
        11 => 30,          # november
        12 => 31           # december
    );
    return $days{$month} if defined($days{$month});

    # if we're here, it's february
    return 29
      unless (
        ( $year % 4 )
        or (!( $year % 100 ) and ( $year % 400 ))
      );
    28;                                                # most februarys have 28
}


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About