Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do i get "the last day of last month"?

by kiseok7 (Beadle)
on Apr 28, 2003 at 03:00 UTC ( [id://253579]=perlquestion: print w/replies, xml ) Need Help??

kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: How do i get "the last day of last month"?
by Juerd (Abbot) on Apr 28, 2003 at 06:23 UTC

    How do i get "the last day of last month"?

    I think that the easiest way to do this involves Date::Tie.

    use Date::Tie; tie my %date, 'Date::Tie'; $date{monthday} = 1; $date{monthday}--; print "$date{year}-$date{month}-$date{monthday}\n";

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: How do i get "the last day of last month"?
by Your Mother (Archbishop) on Apr 28, 2003 at 03:12 UTC
    Here is one way (using today's last month but you could set it otherwise):
    use Date::Calc qw( Today Days_in_Month Day_of_Week Day_of_Week_to_Text Add_Delta_YM ); my ( $year, $month, $date ) = Today(); ( $year, $month, $date ) = Add_Delta_YM( $year, $month, $date, 0, -1 ); my $day = ( Days_in_Month($year,$month) )[-1]; print $day, "\n"; print Day_of_Week_to_Text( Day_of_Week($year,$month,$day) ), "\n";
    Additions above (I realized you might mean day of week and not date)!

    Update 2: D'oh! Re-corrected version to get last month's is now above.

Re: How do i get "the last day of last month"?
by grantm (Parson) on Apr 28, 2003 at 03:37 UTC

    Or, if you only have core modules at your disposal, then you could work out the first of next month and subtract a day:

    use strict; use Time::Local; use constant ONE_DAY => 24 * 60 * 60; my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); $mday = 1; $mon = ($mon + 1) % 12; $year += 1 if($mon == 0); my $time = timelocal($sec,$min,$hour,$mday,$mon,$year) - ONE_DAY; print scalar localtime($time);

    Update: Sorry, I completely misread the question and gave you the last day of this month. The last day of last month is a little easier:

    use Time::Local; use constant ONE_DAY => 24 * 60 * 60; my ($sec,$min,$hour,$mday,$mon,$year) = gmtime(); $mday = 1; my $time = timegm($sec,$min,$hour,$mday,$mon,$year) - ONE_DAY; print "Date: " . localtime($time) . "\n"; $mday = (localtime($time))[3]; print "Day of month: $mday\n";

    Update 2: As MarkM notes below, daylight savings changes could trip these snippets up. I originally coded them to set $sec, $min and $hour all to zero and then subtract say 4 hours - which I think should be pretty safe. I changed it before posting because I thought it was a bit obfuscated - but clear code that's wrong is no less wrong :-)

      I am not particularly fond of this solution as it does not take daylight savings time into account. Sure, most time zones don't adjust their time on the first or last of a month, however, this is not a rule in stone.

      Then again, though, Date::Manip, and a few of the other modules are quite broken when it comes to time zones, so I shouldn't be too critical of a core-perl solution. :-)

Re: How do i get "the last day of last month"?
by PodMaster (Abbot) on Apr 28, 2003 at 11:02 UTC
    How about
    use DateTime 0.10; my $today = DateTime->now(); warn $today->time_zone_short_name(); warn $today->datetime(); $today->subtract( months => 1 ); # or $today->subtract_duration( DateTime::Duration->new( months => 1 ) + ); warn $today->datetime(); my $dt = DateTime->last_day_of_month( year => $today->year, month => $today->month, ); die $dt->datetime(); __END__ UTC at soy.pl line 4. 2003-04-28T11:17:32 at soy.pl line 5. 2003-03-28T11:17:32 at soy.pl line 11. 2003-03-31T00:00:00 at soy.pl line 18.
    See perl http://datetime.perl.org/ for more info.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: How do i get "the last day of last month"?
by TVSET (Chaplain) on Apr 28, 2003 at 03:13 UTC

        Define "simpler" :)

        perl -MDate::Manip -e"print UnixDate( ParseDate( 'last day in' . UnixD +ate( ParseDate('last month'),'%B') ), '%Y/%m/%d' )"

        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.
Re: How do i get "the last day of last month"?
by greenFox (Vicar) on Apr 28, 2003 at 06:14 UTC
    If you are on unix you can use "cal"
    my ($mon,$year) = (localtime())[4,5]; $year +=1900; # perl counts months begining at 0 while cal begins at 1 so # the month returned from perl's localtime is already last # month, we just need to fix for jan $mon = 12 if ! $mon; my $last_day_last_month=(split /\s/, `/bin/cal $mon $year`)[-1];
    I don't believe this solution has any time zone problems either... :)

    --
    Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://253579]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found