caldate_mjd.c (804B)
1 #include <tai/caldate.h> 2 3 static unsigned long times365[4] = { 0, 365, 730, 1095 } ; 4 static unsigned long times36524[4] = { 0, 36524UL, 73048UL, 109572UL } ; 5 static unsigned long montab[12] = 6 { 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337 } ; 7 /* month length after february is (306 * m + 5) / 10 */ 8 9 long caldate_mjd(struct caldate *cd) 10 { 11 long y; 12 long m; 13 long d; 14 15 d = cd->day - 678882L; 16 m = cd->month - 1; 17 y = cd->year; 18 19 d += 146097L * (y / 400); 20 y %= 400; 21 22 if (m >= 2) m -= 2; else { m += 10; --y; } 23 24 y += (m / 12); 25 m %= 12; 26 if (m < 0) { m += 12; --y; } 27 28 d += montab[m]; 29 30 d += 146097L * (y / 400); 31 y %= 400; 32 if (y < 0) { y += 400; d -= 146097L; } 33 34 d += times365[y & 3]; 35 y >>= 2; 36 37 d += 1461L * (y % 25); 38 y /= 25; 39 40 d += times36524[y & 3]; 41 42 return d; 43 }