/****************************************************************************** * This program computes the Ordinal Day of a date. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include #include using namespace std; /****************************************************************************** * Returns true if a year is a leap year. *****************************************************************************/ bool is_leap_year(int Y) noexcept { 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 *****************************************************************************/ int ordinal_date(int M, int D, int Y) noexcept { int od = D; if (M == 2) { od += 31; } else { od += floor(30.6 * M - 91.4); od += is_leap_year(Y) ? 60 : 59; } return od; } int main(int argc, char **argv) { int month = 0; int day = 0; int year = 0; if (argc == 4) { month = Utils::stoiWithDefault(string(argv[1]), 0); day = Utils::stoiWithDefault(string(argv[2]), 0); year = Utils::stoiWithDefault(string(argv[3]), 0); } else { cout << fmt::format("Syntax: {0:s} month day year", argv[0]) << endl; cout << "Syntax: " << argv[0] << " {month} {day} {year}" << endl; exit(1); } int od = ordinal_date(month, day, year); cout << fmt::format("{0:d}/{1:d}/{2:d} = {3:d} Ordinal Date", month, day, year, od) << endl; return 0; }