aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-10-02 23:11:08 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-10-05 18:17:50 +0100
commit7b6c70d9a840776e6078821135771069a0faa098 (patch)
treeffcbe4c715e03f73178fed6f7429e95aad7b4258
parent61e2a29a7e3c0100cbf1560dab538a9fb8e78a66 (diff)
downloadperlweeklychallenge-club-7b6c70d9a840776e6078821135771069a0faa098.tar.gz
perlweeklychallenge-club-7b6c70d9a840776e6078821135771069a0faa098.tar.bz2
perlweeklychallenge-club-7b6c70d9a840776e6078821135771069a0faa098.zip
Solution to task 1
-rwxr-xr-xchallenge-237/jo-37/perl/ch-1.pl65
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-237/jo-37/perl/ch-1.pl b/challenge-237/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..40b6f949b0
--- /dev/null
+++ b/challenge-237/jo-37/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use Date::Manip::Recur;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 4;
+usage: $0 [-examples] [-tests] [YEAR MONTH NTH DOW]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+YEAR MONTH NTH DOW
+ find the day of month for the n-th occurence of the given day of
+ week in year and month.
+
+EOS
+
+
+### Input and Output
+
+say seize_the_day(@ARGV);
+
+
+### Implementation
+
+# Though the primary use of Date::Manip::Recur is the specification of
+# recurring events, it provides a concise description of single events,
+# too. In the context of this task it is "the n-th occurrence of a
+# specific weekday in a given year and month". There is one such event
+# at most, where we pick the day of month from, if it exists.
+sub seize_the_day {
+ local $" = ':';
+ my ($date) = Date::Manip::Recur->new("*@_:0:0:0")->dates;
+ $date ? ($date->value)[2] : 0;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is seize_the_day(2024, 4, 3, 2), 16, 'example 1';
+ is seize_the_day(2025, 10, 2, 4), 9, 'example 2';
+ is seize_the_day(2026, 8, 5, 3), 0, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is seize_the_day(2023, 6, 1, 3), 7, '2023-06-07';
+ }
+
+ done_testing;
+ exit;
+}