blob: 8efb949ed02c5c15db37245a97146ba6aa49474f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2025, Bob Lied
#=============================================================================
# ch-1.pl Perl Weekly Challenge 326 Task 1 Day of Year
#=============================================================================
# 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
#=============================================================================
use v5.40;
use Getopt::Long;
my $Verbose = false;
my $DoTest = false;
GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
my $logger;
{
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ),
layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" });
$logger = Log::Log4perl->get_logger();
}
#=============================================================================
exit(!runTest()) if $DoTest;
say yearDay($_) for @ARGV;
#=============================================================================
sub yearDay($date)
{
use Time::Piece;
return Time::Piece->strptime($date, "%Y-%m-%d")->day_of_year() + 1;
}
sub runTest
{
use Test2::V0;
is( yearDay("2025-02-02"), 33, "Example 1");
is( yearDay("2025-04-10"), 100, "Example 2");
is( yearDay("2025-09-07"), 250, "Example 3");
done_testing;
}
|