// // mjdclock - live MJD clock display. // #include #include #include #define WIN32_LEAN_AND_MEAN #include void main (int argc, char *argv[]) { int dp, mode; char cr; char *ztime (SYSTEMTIME *st); long DateToMjd (long Year, long Month, long Day); // Set MJD decimal places. dp = (argc > 1) ? atoi(argv[1]) : 6; dp = min(dp, 9); dp = max(dp, 0); // Set display mode. mode = (argc > 2) ? atoi(argv[2]) : 1; cr = (mode >= 0) ? '\r' : '\n'; // Display fractional MJD continuously. while (1) { double fMjd, secs; SYSTEMTIME St; time_t t; do { time(&t); GetSystemTime(&St); } while (St.wSecond != (t % 60)); secs = (St.wHour * 60 + St.wMinute) * 60 + St.wSecond; secs += St.wMilliseconds / 1000.0; fMjd = DateToMjd(St.wYear, St.wMonth, St.wDay) + (secs / 86400.0); switch (abs(mode)) { default : case 1 : printf("%.*lf%c", dp, fMjd, cr); break; case 2 : printf("%.24s = %.*lf%c", ctime(&t), dp, fMjd, cr); break; case 3 : printf("%.24s = %s = %.*lf%c", ctime(&t), ztime(&St), dp, fMjd, cr); break; } _sleep(100); // milliseconds } } // Convert calendar date to MJD. long DateToMjd (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; } // Like ctime() except UTC with milliseconds. char *ztime (SYSTEMTIME *st) { static char buf[40]; // STATIC sprintf(buf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d", st->wYear, st->wMonth, st->wDay, st->wHour, st->wMinute, st->wSecond, st->wMilliseconds); return buf; // STATIC }