Last active
February 13, 2020 12:49
-
-
Save fxn/7d4f73bf84187c35b4486e1561877d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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