#!/usr/bin/env perl use utf8; ############################################################################### # This program computes the Ordinal Day of a date. # # Copyright © 2020 Richard Lesh. All rights reserved. ############################################################################### use POSIX qw(floor); use Utils; use strict; use warnings; ############################################################################### # Returns true if a year is a leap year. ############################################################################### sub is_leap_year { my ($Y) = @_; return $Y % 4 == 0 && $Y % 100 != 0 || $Y % 400 == 0; } ############################################################################### # This function computes the Oridnal Date or number of # days starting with January 1 of the year in the proleptic # Gregorian calendar. # Source: https://en.wikipedia.org/wiki/Ordinal_date ############################################################################### sub ordinal_date { my ($M, $D, $Y) = @_; my $od = $D; if ($M == 2) { $od += 31; } else { $od += floor(30.6 * $M - 91.4); $od += is_leap_year($Y) ? 60 : 59; } return $od; } MAIN: { my $month = 0; my $day = 0; my $year = 0; if (scalar(@ARGV) == 3) { $month = Utils::stoiWithDefault($ARGV[0], 0); $day = Utils::stoiWithDefault($ARGV[1], 0); $year = Utils::stoiWithDefault($ARGV[2], 0); } else { print Utils::messageFormat("Syntax: \{0:s\} month day year", "OrdinalDate2"), "\n"; print "Syntax: ", "OrdinalDate2", " \{month\} \{day\} \{year\}\n"; exit 1; } my $od = ordinal_date($month, $day, $year); print Utils::messageFormat("\{0:d\}/\{1:d\}/\{2:d\} = \{3:d\} Ordinal Date", $month, $day, $year, $od), "\n"; }