aboutsummaryrefslogtreecommitdiff
path: root/challenge-013
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-17 17:16:12 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-17 17:16:12 +0100
commit80af7bd3f960b97c76a2eea0937a650c959638f4 (patch)
tree6ee12974bdf7d518c9e865ec093e4abb74fb8d4c /challenge-013
parentd47108fb2faeb3b23f6ad108fc6685586d87b88c (diff)
downloadperlweeklychallenge-club-80af7bd3f960b97c76a2eea0937a650c959638f4.tar.gz
perlweeklychallenge-club-80af7bd3f960b97c76a2eea0937a650c959638f4.tar.bz2
perlweeklychallenge-club-80af7bd3f960b97c76a2eea0937a650c959638f4.zip
- Added solution by Pete Houston.
Diffstat (limited to 'challenge-013')
-rw-r--r--challenge-013/pete-houston/perl5/ch-1.pl53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-013/pete-houston/perl5/ch-1.pl b/challenge-013/pete-houston/perl5/ch-1.pl
new file mode 100644
index 0000000000..f9915c59e7
--- /dev/null
+++ b/challenge-013/pete-houston/perl5/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: friday.pl
+#
+# USAGE: ./friday.pl [ YEAR ]
+#
+# DESCRIPTION: Given a year as argument prints a chronological list of
+# the last Fridays in each month of that year. Defaults to current year
+# if argument is missing or invalid
+#
+#===============================================================================
+
+use strict;
+use warnings;
+
+use Time::Piece;
+use Time::Seconds;
+
+my $year = get_year ();
+
+for my $mon (1..12) {
+ print last_friday($year, $mon) . "\n";
+}
+
+sub get_year {
+ my $year = shift @ARGV;
+ if (defined $year) {
+ if ($year =~ /^(\d{4})$/) {
+ if ($year > 1969 && $year < 2039) {
+ return $1;
+ } else {
+ warn "Year $year outside acceptable range 1970-2038\n";
+ }
+ } else {
+ warn "The supplied year ($year) is invalid. Using current year\n";
+ }
+ } else {
+ warn "No year supplied. Using current year\n";
+ }
+ return localtime(time)->year;
+}
+
+sub last_friday {
+ my ($year, $mon) = @_;
+ my $first = sprintf "%i-%2.2i-01", $year, $mon;
+ my $day = Time::Piece->strptime ($first, "%Y-%m-%d");
+ $day += ONE_DAY * ($day->month_last_day - 1);
+
+ while ($day->day_of_week != 5) { $day -= ONE_DAY }
+ return $day->ymd;
+}
+