Jul2greg
// // jul2greg() -- convert from base date to month/day/year // It takes raw Julian Date, not the modified one. // // The Julian Day (JD) is a continuous day count from Friday, // 3 Febuary 4712 B.C. // The Modified Julian Day (MJD) is the Julian Day minus 2400000. // So the zero date for MJD is Wednesday, 17 November 1858. Until // Sunday, 31 August 2132, when the Julian Date rolls over to // 2500000. // // Years AD are postitive. Years BC are negative. void jul2greg( long julian, // base date long &month, // month.. long &day, // ..day long &year, // ..year with century long &dayofweek, long &dayofyear) // day of week, 0=Sunday { long dd = 0; // work variable int leapyearflag = 0; // leapyear flag julian -= 1721118L; // base calcs on 3/1/1 BC dayofweek = (julian - 5) % 7; // compute day of week year = ((4 * julian - 1) / 146097L); // get century number julian = 4 * julian - 1 - (146097L * year); // ..remove that many days dd = julian / 4; // get to the year julian = (4 * dd + 3) / 1461; // ..within the century year = (100 * year + julian); // ..then year with century dd = 4 * dd + 3 - 1461 * julian; // get to days within 4 yrs leapyearflag = ((dd % 4) == 3) ? 1 : 0; // set leapyear flag dd = (dd + 4) / 4; // get days within base yr month = (5 * dd - 3) / 153; // get month dayofyear = (dd + 59 + leapyearflag); // get day in calendar year dd = 5 * dd - 3 - (153 * month); // get to the day .. day = (dd + 5) / 5; // ..within the month if( month < 10) // q. need to adjust month? { month += 3; // a. yes .. normalize nbr } else { month -= 9; // adjust for March base year++; // ..date and fix year too dayofyear %= (365 + leapyearflag); // ..and day in year } }
Internal Links
Parent Article: Programming Portfolio