#include #include #include // Gregorian Calendar started on Julian Day 2299161 const long gregorianEpoch = 2299161L; const int nMonths = 12; static int monthDays[nMonths + 1] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; void jul2greg( long jul, int & day, int & month, int & year ) { // From "Software, Practice and Experience", Vol. 23 (1993), // pag. 384 if (jul < gregorianEpoch) { std::ostringstream s; s << "Julian Day " << jul << " comes before Gregorian Calendar"; throw std::domain_error(s.str()); } // JD 1721425 corresponds to a (virtual) January 1, year 1 of the // Gregorian Calendar. n400 is the number of completed 400 year // cycles; n100 the number of completed 100 year cycles not included // in n400; n4 the number of completed 4 year cycles not included in // n400 and n100; n1 the number of years not included in n400, n100 // or n4. jul -= 1721426; long n400 = jul / 146097; jul %= 146097; long n100 = jul / 36524; jul %= 36524; long n4 = jul / 1461; jul %= 1461; long n1 = jul / 365; jul %= 365; year = 400 * n400 + 100 * n100 + 4 * n4 + n1; if (n100 == 4 || n1 == 4) { // Day 366 of year "year" day = 31; month = 12; } else { // Day "jul+1" of year "year+1" if (isLeap(++year)) monthDays[2] = 29; else monthDays[2] = 28; ++jul; for (int i = 1; i <= nMonths; ++i) { if (jul <= monthDays[i]) { day = jul; month = i; return; } jul -= monthDays[i]; } std::cerr << "Can't happen\n"; } }