// // Crude test - adjust the PC clock N seconds per day. // #include #include #include char Usage[] = "Usage: perday [seconds per day]\n" "\n" "Examples:\n" " perday 1\n" " perday -12\n" " perday 4.32\n" " perday 2000 [ just for demo ]\n"; void ShowSystemTime (char *s, PSYSTEMTIME st, char *t); void main (int argc, char *argv[]) { int bump_milliseconds; double seconds_per_day; long sleep_time; SYSTEMTIME st1, st2, st3; if (argc < 2) { fprintf(stderr, Usage); exit(1); } sscanf(argv[1], "%lf", &seconds_per_day); printf("%.3lf seconds per day is %.3f ppm\n", seconds_per_day, 1e6 * seconds_per_day / 86400.0); // // Wake up every now and then and adjust the PC clock // by 1/4 second. // // Adjusting by more than one second may cause discontinuous // time stamps for some apps and adjusting less than 50 ms // may be null due to the finite resolution of the PC clock. // So 1/4 second seems fair. // // Also try to adjust in between seconds to reduce the chance // of rollover. // // Note: This tool is not reliable on a heavily loaded system. // bump_milliseconds = 250; sleep_time = (long) (86400.0 / seconds_per_day / (1e3 / bump_milliseconds)); if (sleep_time < 0) { sleep_time *= -1; bump_milliseconds *= -1; } printf("Will adjust PC clock about %d ms every %ld seconds\n", bump_milliseconds, sleep_time); while (1) { while (1) { GetSystemTime(&st1); if (st1.wMilliseconds >= 400 && st1.wMilliseconds <= 600) { st2 = st1; st2.wMilliseconds += bump_milliseconds; SetSystemTime(&st2); GetSystemTime(&st3); break; } Sleep(10); } ShowSystemTime("was ", &st1, "\n"); ShowSystemTime("set ", &st2, "\n"); ShowSystemTime("now ", &st3, "\n"); printf("sleeping...\n"); Sleep(sleep_time * 1000); } exit(0); } void ShowSystemTime (char *s, PSYSTEMTIME st, char *t) { printf("%s%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d UTC%s", s, st->wYear, st->wMonth, st->wDay, st->wHour, st->wMinute, st->wSecond, st->wMilliseconds, t); return; }