Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: How to find the last date of the previous month?

by tobyink (Abbot)
on Oct 30, 2012 at 08:11 UTC ( #1001468=note: print w/replies, xml ) Need Help??

in reply to How to find the last date of the previous month?

In the spirit of TIMTOWTDI, here's an example using Time::Piece. The advantage of using Time::Piece is that as of Perl 5.10, it's bundled with Perl core. (As is its companion module Time::Seconds.)

use 5.010; use Time::Piece; use Time::Seconds qw( ONE_DAY ONE_HOUR ); sub last_day_of_last_month () { my $now = localtime; my $day = Time::Piece->new( $now - ($now->day_of_month * ONE_DAY) ); $day += ONE_HOUR * ($now->isdst <=> $day->isdst); return $day; } say last_day_of_last_month->mdy('/');

The daylight savings calculation is a bit of an annoyance. :-(

Yeah, that's right; I just did maths with the spaceship operator! :-)

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001468]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-02-20 22:03 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.