aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-29 14:14:35 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-08-01 08:57:57 +0200
commitf506c15b51deb761384d33fce37dc14fc715df53 (patch)
tree9f9eceb5a162472444faa3acb89e2f719b8187fc
parent6f0d16f05f2773a17829abb2db30dff2c2f73444 (diff)
downloadperlweeklychallenge-club-f506c15b51deb761384d33fce37dc14fc715df53.tar.gz
perlweeklychallenge-club-f506c15b51deb761384d33fce37dc14fc715df53.tar.bz2
perlweeklychallenge-club-f506c15b51deb761384d33fce37dc14fc715df53.zip
Solution to task 1
-rwxr-xr-xchallenge-332/jo-37/perl/ch-1.pl95
1 files changed, 95 insertions, 0 deletions
diff --git a/challenge-332/jo-37/perl/ch-1.pl b/challenge-332/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..dbec5e1341
--- /dev/null
+++ b/challenge-332/jo-37/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use Date::Manip::Date;
+
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV;
+
+sub usage {
+ die <<~EOS;
+ $0 - binary date
+
+ usage: $0 [-examples] [-tests] [DATE]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ DATE
+ a date in any format parsable by Date::Manip::Date::parse_date,
+ e.g. YYYYMMDD, YYMMDD, YYYY-MM-DD etc.
+
+ EOS
+}
+
+
+### Input and Output
+
+say binary_date("@ARGV");
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/08/01/ch-332.html#task-1
+
+
+sub binary_date {
+ my $date = Date::Manip::Date->new;
+ $date->parse_date(shift) && die $date->err;
+ sprintf "%b-%b-%b", $date->printf(qw(%Y %f %e));
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = binary_date(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["2025-07-26"], "11111101001-111-11010", 'example 1'],
+ [["2000-02-02"], "11111010000-10-10", 'example 2'],
+ [["2024-12-31"], "11111101000-1100-11111", 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ plan 2;
+ like dies{binary_date("2025-02-29")}, qr/Invalid date/, 'invalid date';
+ is binary_date("250729"), "11111101001-111-11101", 'different format';
+ }) : pass 'skip tests';
+
+ exit;
+}