caldate_fmjd.c (996B)
1 #include <tai/caldate.h> 2 3 void caldate_frommjd(cd,day,pwday,pyday) 4 struct caldate *cd; 5 long day; 6 int *pwday; 7 int *pyday; 8 { 9 long year; 10 long month; 11 int yday; 12 13 year = day / 146097L; 14 day %= 146097L; 15 day += 678881L; 16 while (day >= 146097L) { day -= 146097L; ++year; } 17 18 /* year * 146097 + day - 678881 is MJD; 0 <= day < 146097 */ 19 /* 2000-03-01, MJD 51604, is year 5, day 0 */ 20 21 if (pwday) *pwday = (day + 3) % 7; 22 23 year *= 4; 24 if (day == 146096L) { year += 3; day = 36524L; } 25 else { year += day / 36524L; day %= 36524L; } 26 year *= 25; 27 year += day / 1461; 28 day %= 1461; 29 year *= 4; 30 31 yday = (day < 306); 32 if (day == 1460) { year += 3; day = 365; } 33 else { year += day / 365; day %= 365; } 34 yday += day; 35 36 day *= 10; 37 month = (day + 5) / 306; 38 day = (day + 5) % 306; 39 day /= 10; 40 if (month >= 10) { yday -= 306; ++year; month -= 10; } 41 else { yday += 59; month += 2; } 42 43 cd->year = year; 44 cd->month = month + 1; 45 cd->day = day + 1; 46 47 if (pyday) *pyday = yday; 48 }