in reply to Last day of the month. Any shorter

Note: this code is not "shorter", but it's the code I usually use for this sort of thing.

The way I usually go about determining the last day of a month is:

use Time::Local; ### this will get me the Unix time ### for the last day in May # get today's month and year my ($m, $y) = (localtime)[4,5]; # get next month's month and year my ($nm, $ny) = ($m == 11) ? (0, $y+1) : ($m+1, $y); # get noon on the first of next month my $next_month_noon = timelocal(0,0,12, 1,$nm,$ny); # subtract a day from it (86400 seconds) my $month_last_day = $next_month_noon - 86400;
Now I have a time value in $month_last_day that I can pass to localtime() if I want. I can use this code, then, to determine if a given date is the last day of its month:
# my code above, as a function sub lastday_time { use Time::Local; # I never do this in real code # I put module uses at the top of # the entire program, not each sub # arguments are assumed to be in "human" terms my ($m, $y) = @_; $m--, $y -= 1900; # perl-ify them my ($nm, $ny) = ($m == 11) ? (0,$y+1) : ($m+1,$y); # return the first of next month, minus one day return timelocal(0,0,12, 1,$nm,$ny) - 86400; } sub is_last_of_month { # arguments are "human" # lastday_time() subtracts 1 from $m and 1900 from $y my ($d, $m, $y) = @_; my $last_day = (localtime lastday_time($m,$y))[3]; # fixed that last line -- I had $d, meant $m return $d == $last_day; }
There, the is_last_of_month() function returns true if the given date (like (31,5,2004)) is the last of that month, and false otherwise.
_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;