Skip to content

Instantly share code, notes, and snippets.

@fxn
Last active February 13, 2020 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxn/7d4f73bf84187c35b4486e1561877d69 to your computer and use it in GitHub Desktop.
Save fxn/7d4f73bf84187c35b4486e1561877d69 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
int main(void)
{
struct timeval now;
double seconds;
gettimeofday(&now, NULL);
/*
To keep the implementation simple, we assume the absolute value of the
drift is less than 30s and work with the distance to the current minute.
This is enough for me, but can be improved by asking the user which was
the time in their watch.
*/
seconds = (now.tv_sec % 60) + now.tv_usec/1e6;
if (seconds < 30) {
/* Past real 00s means we are late, the watch is slow. */
printf("-%0.2f\n", seconds);
} else {
/* Otherwise, the watch is on time, or going too fast. */
printf("+%0.2f\n", 60 - seconds);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment