<?xml version="1.0" encoding="windows-1252"?>
<node id="1001468" title="Re: How to find the last date of the previous month?" created="2012-10-30 04:11:28" updated="2012-10-30 04:11:28">
<type id="11">
note</type>
<author id="757127">
tobyink</author>
<data>
<field name="doctext">
&lt;p&gt;In the spirit of TIMTOWTDI, here's an example using [mod://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 [mod://Time::Seconds].)&lt;/p&gt;

&lt;code&gt;
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-&gt;new(
		$now - ($now-&gt;day_of_month * ONE_DAY)
	);
	$day += ONE_HOUR * ($now-&gt;isdst &lt;=&gt; $day-&gt;isdst);
	return $day;
}

say last_day_of_last_month-&gt;mdy('/');
&lt;/code&gt;

&lt;p&gt;The daylight savings calculation is a bit of an annoyance. :-(&lt;/p&gt;

&lt;p&gt;Yeah, that's right; I just did maths with the spaceship operator! :-)&lt;/p&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-757127"&gt;
&lt;small&gt;&lt;small&gt;
&lt;tt&gt;perl -E'sub Monkey::do{say$_,for@_,do{($monkey=&amp;#x5B;caller(0)]-&gt;&amp;#x5B;3])=~s{::}{ }and$monkey}}"Monkey say"-&gt;Monkey::do'
&lt;/tt&gt;&lt;/small&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1001445</field>
<field name="parent_node">
1001445</field>
</data>
</node>
