Created
March 18, 2014 13:22
-
-
Save diabloneo/9619917 to your computer and use it in GitHub Desktop.
Calculate diff of two struct timespec
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 <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; | |
} |
timerspecsub
nor timersub
are part of any standard.
timerspecsub
nortimersub
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
Thanks for that info. I just discovered that
<sys/time.h>
also hastimespecsub()
so you don't even need to modify it. It is a BSD function (actually a macro) and not a POSIX one. However, in linux we have libbsd to provide it.The man pages includes many functions (or macros) which may also be interesting to you:
man timespecsub