Here's my refactored version of your demonstration script:
use strict;
use warnings;
use Time::Piece;
my $year = shift @ARGV || 2014;
my $p = '%Y/%m/%d'; # strptime() format
my $f = '%a %b %d %Y'; # strftime() format
my $t = Time::Piece->new();
for my $month (1 .. 12) {
# Compute the last day of the month...
my $monthLastDay
= $t->strptime("$year/$month/1", $p)->month_last_day();
# Compute the day of the week of the last day of the month (Sunday
+ = 1)...
my $weekDay
= $t->strptime("$year/$month/$monthLastDay", $p)->wday();
# Compute the day of the last Friday of the month (Friday = 6)...
my $lastFridayDate
= $monthLastDay - (($weekDay - 6) % 7);
# Compute the last Friday of the month...
my $lastFriday
= $t->strptime("$year/$month/$lastFridayDate", $p)->strftime($
+f);
print "$lastFriday\n";
}
exit 0;
__END__
Fri Jan 31 2014
Fri Feb 28 2014
Fri Mar 28 2014
Fri Apr 25 2014
Fri May 30 2014
Fri Jun 27 2014
Fri Jul 25 2014
Fri Aug 29 2014
Fri Sep 26 2014
Fri Oct 31 2014
Fri Nov 28 2014
Fri Dec 26 2014
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|