Fix an issue where access.log would have the wrong timestamp.
[cubemap] / timespec.cpp
1 #include <assert.h>
2 #include <sys/time.h>
3
4 #include "timespec.h"
5
6 timespec clock_diff(const timespec &a, const timespec &b)
7 {
8         timespec ret;
9         ret.tv_sec = b.tv_sec - a.tv_sec;
10         ret.tv_nsec = b.tv_nsec - a.tv_nsec;
11         if (ret.tv_nsec < 0) {
12                 ret.tv_sec--;
13                 ret.tv_nsec += 1000000000;
14         }
15         assert(ret.tv_nsec >= 0);
16         return ret;
17 }
18
19 timespec clock_add(const timespec &a, const timespec &b)
20 {
21         timespec ret;
22         ret.tv_sec = a.tv_sec + b.tv_sec;
23         ret.tv_nsec = b.tv_nsec + b.tv_nsec;
24
25         ret.tv_sec += ret.tv_nsec / 1000000000;
26         ret.tv_nsec %= 1000000000;
27         return ret;
28 }