In the program that accumulates the delta struct timespec, I do the following logic:
struct timespec accu, start, stop;
for (...) {
accu.tv_sec += stop.tv_sec - start.tv_sec;
accu.tv_nsec += stop.tv_nsec - start.tv_nsec;
if (accu.tv_nsec >= 1000000000L) {
accu.tv_nsec -= 1000000000L;
++accu.tv_sec;
}
}
However, when I print the result using:
printf("%lld.%.9ld\n", (long long) accu.tv_sec, accu.tv_nsec);
Do I ever see results like: 1.-611075708
What am I doing wrong to have a negative value in accu.tv_nsec?
Note: startand stopextracted using clock_gettime().
source
share