// CalendarInfo implementation (1992, Information Disciplines, Inc.) // --------------------------- (See CalendarInfo.hpp for details.) #include "global.hpp" #include "CalendarInfo.hpp" /* Declarations, also included by users */ // Calendar arithmetic functions // ----------------------------- bool CalendarInfo::isLeapYear (INT yyyy) {return (0 == yyyy % 400) || ((0 == yyyy % 4) && (0 != yyyy % 100));} bool CalendarInfo::isLegalYMD (INT yyyy, SHORT mm, SHORT dd) {return mm > 0 && mm <= 12 && dd > 0 && (dd <= DAYS_IN_MONTH[mm] || (dd == 29 && mm == 2 && isLeapYear(yyyy))); } short CalendarInfo::dayNumber // Convert year-month-day (INT yyyy, INT mm, INT dd) // to year and day number {if (!isLegalYMD(yyyy, mm, dd)) return 0;// (Sometimes mistakenly int ddd = DAYS_BEFORE_MONTH[mm] + dd; // called a "Julian date") if (isLeapYear(yyyy) && mm > 2) ++ddd; return ddd; } // This routine performs the inverse of the above dayNumber function. // (No error checking -- result undefined for day number out of range) void CalendarInfo::ymd (INT yyyy_in, short& mm_out, short& ddd_in_out) {int dx = ddd_in_out; if (isLeapYear(yyyy_in)) // For leap years if (ddd_in_out > 60) --dx; // adjust day after 29 Feb., else if (ddd_in_out == 60) // Test for 29 Feb. special case {mm_out = 2; ddd_in_out = 29; return;} mm_out = (dx + 28) / 29; // Estimate the month, then adjust if (mm_out == 13 || dx <= DAYS_BEFORE_MONTH[mm_out]) --mm_out; ddd_in_out = dx - DAYS_BEFORE_MONTH[mm_out];// Compute day of month cout << "Result is " << mm_out << '-' << ddd_in_out << endl; return; } // Conversion functions to external representations // -------------------- SimpleString CalendarInfo::toString(INT yyyy, SHORT mm, SHORT dd) {return SimpleString::toString(yyyy,10) + '-' + SimpleString::toString(mm,10) + '-' + SimpleString::toString(dd,10); } SimpleString CalendarInfo::toEnglish // Date to American English (INT yyyy, SHORT mm, SHORT dd) // conversion {return SimpleString(MONTH_NAME[mm]) + ' ' + SimpleString::toString(dd,10) + ", " + SimpleString::toString(yyyy,10); }