Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Forked from diabloneo/timespec_diff.c
Created August 26, 2017 03:33
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 devendranaga/33dfc4354d33edf31e603734d43c9a49 to your computer and use it in GitHub Desktop.
Save devendranaga/33dfc4354d33edf31e603734d43c9a49 to your computer and use it in GitHub Desktop.
Calculate diff of two struct timespec
#include <time.h>
void timespec_diff(struct timespec *start, struct timespec *stop,
struct timespec *result)
{
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
} else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment