aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-06-19 18:38:45 +0200
committerGitHub <noreply@github.com>2025-06-19 18:38:45 +0200
commit399f612279e4bc81edfe0a21119cbe727d3a65c8 (patch)
tree904f889c1eac0d6a8e64d903da48bf1bdd96c05e
parentcf38a6108a1b0635a8c9375c3eea633fe833ca1a (diff)
downloadperlweeklychallenge-club-399f612279e4bc81edfe0a21119cbe727d3a65c8.tar.gz
perlweeklychallenge-club-399f612279e4bc81edfe0a21119cbe727d3a65c8.tar.bz2
perlweeklychallenge-club-399f612279e4bc81edfe0a21119cbe727d3a65c8.zip
Create ch-1.pl
-rw-r--r--challenge-326/wanderdoc/perl/ch-1.pl45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-326/wanderdoc/perl/ch-1.pl b/challenge-326/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..8b7f83300f
--- /dev/null
+++ b/challenge-326/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a date in the format YYYY-MM-DD.
+Write a script to find day number of the year that the given date represent.
+
+Example 1
+
+Input: $date = '2025-02-02'
+Output: 33
+
+The 2nd Feb, 2025 is the 33rd day of the year.
+
+
+Example 2
+
+Input: $date = '2025-04-10'
+Output: 100
+
+
+Example 3
+
+Input: $date = '2025-09-07'
+Output: 250
+
+=cut
+
+
+
+use Time::Piece;
+use Test2::V0 -no_srand => 1;
+is(str_to_day_of_year('2025-02-02'), 33, 'Example 1');
+is(str_to_day_of_year('2025-04-10'), 100, 'Example 2');
+is(str_to_day_of_year('2025-09-07'), 250, 'Example 3');
+done_testing();
+
+sub str_to_day_of_year
+{
+ my $string = $_[0];
+ my $format = '%Y-%m-%d';
+ my $dt = Time::Piece->strptime($string, $format);
+ return $dt->day_of_year + 1; # $dt->day_of_year: 0 = Jan 01
+}