// Testing if a Common-Era date is valid // // - This exercise continues from the previous one // that simply checks if a year is a leap year. // // A tropical year, which measures the cycle of the seasons, is approximately // 365.2421897 days, or 31,556,925.19008 seconds. This is the average time // required for the seasons to repeat, although the actual intervals between // successive solstices and equinoxes vary slightly. // // If a calendar had exactly 365 days every year, the seasons would gradually // drift to later calendar dates. Approximately every four years, the summer // and winter solstices would occur one day later. After a century, they would // occur approximately 24 days later. Thus, if a culture taught its farmers to // plant their crops on May 1 to avoid frost, after a century, May 1 would be // almost a month too early relative to the seasons. // // The ancient Romans recognized that a year was closer to 365.25 days. // In 45 BCE, Julius Caesar introduced the Julian calendar, which included an // additional day every four years. However, this overestimated the tropical // year by 0.25 − 0.2421897 = 0.0078103 days, or approximately 675 seconds // per year. Consequently, the calendar drifted by approximately one day every // 128 years, as 10.0078103≈128.035. // // By 1582, this discrepancy had accumulated to approximately ten days. // Pope Gregory XIII introduced the Gregorian calendar, which omitted ten // calendar dates and modified the leap-year rule: years divisible by 100 would // no longer be leap years unless they were also divisible by 400. Thus, every // 400 years would contain 97 leap years, giving an average year length of // 365.2425 days. This differs from the tropical year by only approximately // 27 seconds per year. // // Although keeping the calendar synchronized with the seasons was important for // agriculture, the principal motivation for the Gregorian reform was religious: // restoring the March equinox to approximately March 21 so that Easter could be // calculated according to the established ecclesiastical rules. // // Incidentally, had leap years been omitted once every 128 years, rather than // three times every 400 years, the average calendar year would have been // 365.2421875 days, differing from the tropical year by only approximately // 0.19 seconds per year. This is considerably more accurate than the Gregorian // calendar, although the length of the tropical year changes slowly over time. // // Important: In your code, you should use the local variables assigned above // and not numbers like 1582. This way, the reader does not see a test like // if ( year < 1582 ) {...} // and wonder what the significance of the number is. The condition // if ( year < greg_first_year ) {...} // shows the intent of the programmer. int main() { // Test if a year of the Common Era is valid // // A year 'y' in the Common Era is valid if 'y > 0'. // // Query the user for a year, assign it to 'year', and if it is not a // valid year of the Common Era, print "Invalid Common-Era year" to the // screen and return -1. // Test if a year of the Common Era is a leap year // // A year 'y' in the Common Era is a leap year if it is either before 1582 // and divisible by 4 or after 1582 and divisible by 4 but not if it is // divisible by 100 unless it is also divisble by 400. // // Set the Boolean variable leap_year to true if the year year is a leap // year and false otherwise. // - Note that 1582 is not divisble by 4. // Test if an integer represents a valid month: // // The integers 1 through 12 represent the months January through December, // respectively. All other integers are invalid. // // Query the user for a month, assign it to month, and if it is not valid, // print "Invalid month" to the screen and return -1. // Test if an integer represents a valid day of the month // // The integers 1 through 31 represent valid days in January, March, May, // July, August, October, and December (1, 3, 5, 7, 8, 10, 12). // The integers 1 through 30 represent valid days in April, June, September, // and November (4, 6, 9, 11). // If the month is February, then the month has 28 days if it is not a leap // year and 29 days if it is. // // Query the user for a day of the month, assign it to day, and if it is not // valid, print "Invalid day" to the screen and return -1. // Check if the date is in the Gregorian calendar // // Next, the first date of the Gregorian calendar was October 15, 1582, so // initialize these local variables: int greg_first_year{ 1582 }; int greg_first_month{ 10 }; int greg_first_day{ 15 }; int julian_last_day{ 4 }; // If the entered date is equal to this date, display: // "The date is the first day of the Gregorian calendar.", // and if it is after, print // "The date is after the start of the Gregorian calendar." // Otherwise, the last day of the Julian calendar was October 4, 1582, // so if the falls between October 5, 1582 and October 14, 1582, display // to the screen // "Invalid calendrical date." // and return -1. // // Otherwise, the date is a Julian calenar date, so display to the screen // "The date is during the era of the Julian calendar." // To save time, a skeleton is provided for you: if ( false ) { std::cout << "The date is the first day of the Gregorian calendar." << std::endl; } else if ( false ) { std::cout << "The date is after the start of the Gregorian calendar." << std::endl; } else if ( false ) { std::cout << "Invalid calendrical date." << std::endl; } else { std::cout << "The date is during the era of the Julian calendar." << std::endl; } return 0; }