// // tomjd -- Convert date and time to MJD (Modified Julian Day) // // 25-Dec-2010 tvb www.LeapSecond.com/tools // #include #include #include long date_to_mjd (long year, long month, long day); void main (int argc, char *argv[]) { long mjd, stod, year, month, day, hour, minute, second; double fmjd; // Get user specified date and time. if (argc > 1) { year = month = day = hour = minute = second = 0; sscanf(argv[1], "%ld-%ld-%ld %ld:%ld:%ld", &year, &month, &day); if (argc > 2) { sscanf(argv[2], "%ld:%ld:%ld", &hour, &minute, &second); } } else { // By default use current date and time. time_t t = time(NULL); struct tm *tm = gmtime(&t); year = tm->tm_year + 1900; month = tm->tm_mon + 1; day = tm->tm_mday; hour = tm->tm_hour; minute = tm->tm_min; second = tm->tm_sec; printf("%.24s (current local time)\n", ctime(&t)); } // Convert date to integer MJD (Modified Julian Day). // Convert time to integer STOD (Seconds Time Of Day). // Combine to form floating point MJD. mjd = date_to_mjd(year, month, day); stod = (((hour * 60) + minute) * 60) + second; fmjd = mjd + (stod / 86400.0); printf("%.4ld-%.2ld-%.2ld %.2ld:%.2ld:%.2ld UTC = MJD %ld + STOD %ld = MJD %.6lf\n", year, month, day, hour, minute, second, mjd, stod, fmjd); } // // Return Modified Julian Day (MJD) given // calendar year, month (1-12), and day (1-31). // - Valid for Gregorian dates from 17-Nov-1858. // - Adapted from sci.astro FAQ. // long date_to_mjd (long year, long month, long day) { return 367 * year - 7 * (year + (month + 9) / 12) / 4 - 3 * ((year + (month - 9) / 7) / 100 + 1) / 4 + 275 * month / 9 + day + 1721028 - 2400000; } #if 0 >tomjd 2010-12-24 22:13:41 UTC = MJD 55554 + STOD 80021 = MJD 55554.926169 >tomjd 2000-1-1 2000-01-01 00:00:00 UTC = MJD 51544 + STOD 0 = MJD 51544.000000 >tomjd 2010-12-25 2010-12-25 00:00:00 UTC = MJD 55555 + STOD 0 = MJD 55555.000000 >tomjd 2010-12-25 13:20 2010-12-25 13:20:00 UTC = MJD 55555 + STOD 48000 = MJD 55555.555556 #endif