// Routines for performing computations on dates. In these routines, // months are expressed as integers from 1 to 12, days are expressed // as integers from 1 to 31, and years are expressed as 4-digit // integers. #include #include using namespace std; string dayOfWeek[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; // converts Gregorian date to integer (Julian day number) int dateToInt (int m, int d, int y){ return 1461 * (y + 4800 + (m - 14) / 12) / 4 + 367 * (m - 2 - (m - 14) / 12 * 12) / 12 - 3 * ((y + 4900 + (m - 14) / 12) / 100) / 4 + d - 32075; } // converts integer (Julian day number) to Gregorian date: month/day/year void intToDate (int jd, int &m, int &d, int &y){ int x, n, i, j; x = jd + 68569; n = 4 * x / 146097; x -= (146097 * n + 3) / 4; i = (4000 * (x + 1)) / 1461001; x -= 1461 * i / 4 - 31; j = 80 * x / 2447; d = x - 2447 * j / 80; x = j / 11; m = j + 2 - 12 * x; y = 100 * (n - 49) + i + x; } // converts integer (Julian day number) to day of week string intToDay (int jd){ return dayOfWeek[jd % 7]; } int main (int argc, char **argv){ int jd = dateToInt (3, 24, 2004); int m, d, y; intToDate (jd, m, d, y); string day = intToDay (jd); // expected output: // 2453089 // 3/24/2004 // Wed cout << jd << endl << m << "/" << d << "/" << y << endl << day << endl; }