Skip to content

Instantly share code, notes, and snippets.

@diabloneo
Created March 18, 2014 13:22
Show Gist options
  • Save diabloneo/9619917 to your computer and use it in GitHub Desktop.
Save diabloneo/9619917 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;
}
@markand
Copy link

markand commented Aug 8, 2022

timerspecsub nor timersub are part of any standard.

@alejandro-colomar
Copy link

alejandro-colomar commented Aug 8, 2022

timerspecsub nor timersub are part of any standard.

Yet they are useful, and relatively portable (Linux and BSDs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment