Skip to content

Instantly share code, notes, and snippets.

@cbhl
Created July 7, 2013 19:42
Show Gist options
  • Save cbhl/5944647 to your computer and use it in GitHub Desktop.
Save cbhl/5944647 to your computer and use it in GitHub Desktop.
Sanity-checking CLOCK_MONOTONIC (v1).
#include <time.h>
#include <stdio.h>
// compile with:
// $ gcc -Wall -o time_loop time_loop.c -lrt
// make sure -lrt comes after specifying the .c file or it won't link
int rv;
struct timespec current;
struct timespec last;
void print_ts() {
//printf("got: %lu.%lu\n", current.tv_sec, current.tv_nsec);
if (current.tv_nsec < last.tv_nsec) {
printf("current: %lu.%lu\n", current.tv_sec, current.tv_nsec);
printf("last: %lu.%lu\n", last.tv_sec, last.tv_nsec);
printf("tv_nsec went backwards");
if (current.tv_sec > last.tv_sec) {
printf("... but tv_sec went forwards so it's okay.");
}
printf("\n");
}
if (current.tv_sec < last.tv_sec) {
printf("tv_sec went backwards\n");
}
}
int main(int argc, char** argv) {
printf("Hello, world!\n");
while (1) {
rv = clock_gettime(CLOCK_MONOTONIC, &current);
if (rv < 0) {
printf("clock_gettime failed!\n");
}
print_ts();
last = current;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment