// // Simple tool to convert fractional MJD to UTC and local time. // Valid for modern MJD (1970 to 2038). // #include #include #include #include char *zonename (char *name) { int i; char *p; static char zone[4]; i = 0; for (p = name; *p && i < 3; p += 1) { if (*p >= 'A' && *p <= 'Z') { zone[i] = *p; i += 1; } } zone[i] = '\0'; return zone; } #define MJD_1970_01_01 40587 #define UNIX_TIME_TO_MJD(t) ( MJD_1970_01_01 + ((t) / 86400.0) ) #define MJD_TO_UNIX_TIME(m) ( (long) (((m) - MJD_1970_01_01) * 86400.0) ) #define SAFE_CTIME(t) ( ctime(t) ? ctime(t) : "null" ) #define SAFE_TM(tm) ( (tm) ? (tm)->tm_isdst : 0 ) void show (time_t Time, double Mjd) { struct tm *tm; int zone; int dst; if (Time == 0) Time = MJD_TO_UNIX_TIME(Mjd); if (Mjd == 0.0) Mjd = UNIX_TIME_TO_MJD(Time); printf("MJD %.6lf =\n", Mjd); zone = _timezone; dst = _daylight; { _timezone = 0; _daylight = 0; printf(" %.24s %s\n", SAFE_CTIME(&Time), "UTC"); } _timezone = zone; _daylight = dst; tm = localtime(&Time); printf(" %.24s %s\n", SAFE_CTIME(&Time), zonename(_tzname[SAFE_TM(tm)])); } void main (int argc, char *argv[]) { double Mjd; time_t Time; if (argc == 1) { time(&Time); show(Time, 0); } else while (argc > 1) { sscanf(argv[1], "%lf", &Mjd); show(0, Mjd); argc -= 1; argv += 1; } }