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:??;

In reply to Re: Last day of the month. Any shorter by japhy
in thread Last day of the month. Any shorter by Scarborough

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.