diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-11-13 14:21:25 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-11-14 10:39:20 +0100 |
| commit | ce75b2541cc3888517a75bc7690a3dd321c1137c (patch) | |
| tree | a9f6b21aa3f63bc1d07407eae00b7609c36e2165 | |
| parent | 349fcb363ce1d6422484849a9a118baff2eb1827 (diff) | |
| download | perlweeklychallenge-club-ce75b2541cc3888517a75bc7690a3dd321c1137c.tar.gz perlweeklychallenge-club-ce75b2541cc3888517a75bc7690a3dd321c1137c.tar.bz2 perlweeklychallenge-club-ce75b2541cc3888517a75bc7690a3dd321c1137c.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-347/jo-37/perl/ch-1.pl | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/challenge-347/jo-37/perl/ch-1.pl b/challenge-347/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..d1e46a64b6 --- /dev/null +++ b/challenge-347/jo-37/perl/ch-1.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use DateTime::Format::Strptime; +use DateTime::Format::DateParse; + +### 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 - format date + + usage: $0 [-examples] [-tests] [DATE] + + -examples + run the examples from the challenge + + -tests + run some tests + + DATE + a date parseable by Date::Parse + + EOS +} + + +### Input and Output + +say format_date("@ARGV"); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/11/14/ch-347.html#task-1 + +sub format_date { + state $fmt = DateTime::Format::Strptime->new(pattern => '%F'); + DateTime::Format::DateParse->parse_datetime(shift)->set_formatter($fmt); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name, $reason=undef) { + my $todo = $reason ? todo $reason : undef; + my $result = format_date(@$args); + is $result, $expected, + "$name: (@$args) -> " . $expected->name; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["1st Jan 2025"], string("2025-01-01"), 'example 1'], + [["22nd Feb 2025"], string("2025-02-22"), 'example 2'], + [["15th Apr 2025"], string("2025-04-15"), 'example 3'], + [["23rd Oct 2025"], string("2025-10-23"), 'example 4'], + [["31st Dec 2025"], string("2025-12-31"), 'example 5'], + ); + plan scalar @examples; + run_example @$_ for @examples; + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 1; + like dies {format_date("29th Feb 2025")}, qr/invalid/i, 'invalid date'; + }) : pass 'skip tests'; + + exit; +} |
