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

Re: Gettting the last Friday of every month using Time::Piece

by gurpreetsingh13 (Scribe)
on Jul 01, 2014 at 10:02 UTC ( #1091819=note: print w/replies, xml ) Need Help??


in reply to Gettting the last Friday of every month using Time::Piece

Please check if this works for you. Simple solution to work for each month in reverse direction.

use v5.14; #!/usr/bin/perl use strict; use warnings; use Time::Piece; my $t = Time::Piece->new(); my $year = $ARGV[0] || 2014; foreach my $month ( 1 .. 12 ) { #Make an object with first day of each month and use it to get las +t day of each month my $tFirstDayObject = $t->strptime( "$year/$month/1", "%Y/%m/%d" ) +; my $monthLastDay = $tFirstDayObject->month_last_day; #Now make an object with this last date and use it to get last wee +kDay number my $tLastDayObject = $t->strptime( "$year/$month/$monthLastDay", " +%Y/%m/%d" ); my $weekDay = $tLastDayObject->wday; #Now calculate how many days to add in this weekDay taking Sunday +as the first day my $daysToMoveBack = 0; $weekDay >= 6 ? ( $daysToMoveBack = $weekDay - 6 ) : ( $daysToMove +Back = $weekDay + 1 ); my $lastFridayDate = $monthLastDay - $daysToMoveBack; #Now make an object with this $lastFridayDate for each month my $tLastFridayObject = $t->strptime( "$year/$month/$lastFridayDat +e", "%Y/%m/%d" ); say $tLastFridayObject->cdate; }
Output:
Fri Jan 31 00:00:00 2014
Fri Feb 28 00:00:00 2014
Fri Mar 28 00:00:00 2014
Fri Apr 25 00:00:00 2014
Fri May 30 00:00:00 2014
Fri Jun 27 00:00:00 2014
Fri Jul 25 00:00:00 2014
Fri Aug 29 00:00:00 2014
Fri Sep 26 00:00:00 2014
Fri Oct 31 00:00:00 2014
Fri Nov 28 00:00:00 2014
Fri Dec 26 00:00:00 2014

Replies are listed 'Best First'.
Re^2: Gettting the last Friday of every month using Time::Piece
by Jim (Curate) on Jul 01, 2014 at 17:47 UTC

    ++gurpreetsingh13. By your use of meaningful variable names and explanatory comments, your script explains what it does and how it does it. One is able to understand your algorithm by reading just the script itself, without having to refer to any module documentation.

    This…

    my $daysToMoveBack = 0; $weekDay >= 6 ? ( $daysToMoveBack = $weekDay - 6 ) : ( $daysToMove +Back = $weekDay + 1 );

    …is more succinctly written like this…

    my $daysToMoveBack = $weekDay >= 6 ? $weekDay - 6 : $weekDay + 1;
      Great thanks for the correction and improvement.
Re^2: Gettting the last Friday of every month using Time::Piece
by Jim (Curate) on Jul 01, 2014 at 21:07 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1091819]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2021-04-13 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?