summaryrefslogtreecommitdiff
path: root/src/time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/time.c')
-rw-r--r--src/time.c62
1 files changed, 16 insertions, 46 deletions
diff --git a/src/time.c b/src/time.c
index e7eee3f..4daef27 100644
--- a/src/time.c
+++ b/src/time.c
@@ -6,6 +6,7 @@
* General Public License as published by the Free Software Foundation;
* either version 2.1 or (at your option) any later version.
*/
+#include <stdint.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdbool.h>
@@ -39,38 +40,13 @@ HX_timespec_neg(struct timespec *r, const struct timespec *a)
EXPORT_SYMBOL struct timespec *HX_timespec_add(struct timespec *r,
const struct timespec *a, const struct timespec *b)
{
- /*
- * Split the value represented by the struct into two
- * independent values that can be added individually.
- */
- long nsec[2];
- nsec[0] = (a->tv_sec < 0) ? -a->tv_nsec : a->tv_nsec;
- nsec[1] = (b->tv_sec < 0) ? -b->tv_nsec : b->tv_nsec;
-
- r->tv_sec = a->tv_sec + b->tv_sec;
- r->tv_nsec = nsec[0] + nsec[1];
- if (r->tv_nsec >= NANOSECOND) {
- ++r->tv_sec;
- r->tv_nsec -= NANOSECOND;
- } else if (r->tv_nsec <= -NANOSECOND) {
- --r->tv_sec;
- r->tv_nsec += NANOSECOND;
- }
-
- /* Combine again */
- if (r->tv_sec < 0) {
- if (r->tv_nsec < 0) {
- r->tv_nsec = -r->tv_nsec;
- } else if (r->tv_nsec > 0) {
- if (++r->tv_sec == 0)
- r->tv_nsec = -NANOSECOND + r->tv_nsec;
- else
- r->tv_nsec = NANOSECOND - r->tv_nsec;
- }
- } else if (r->tv_sec > 0 && r->tv_nsec < 0) {
- --r->tv_sec;
- r->tv_nsec = NANOSECOND + r->tv_nsec;
- }
+ long aa = a->tv_sec * NANOSECOND_LL +
+ ((a->tv_sec >= 0) ? a->tv_nsec : -a->tv_nsec);
+ long bb = b->tv_sec * NANOSECOND_LL +
+ ((b->tv_sec >= 0) ? b->tv_nsec : -b->tv_nsec);
+ long rr = aa + bb;
+ r->tv_sec = rr / NANOSECOND;
+ r->tv_nsec = ((r->tv_sec < 0) ? -rr : rr) % NANOSECOND;
return r;
}
@@ -119,20 +95,14 @@ HX_timespec_mulf(struct timespec *r, const struct timespec *a, double f)
EXPORT_SYMBOL struct timeval *HX_timeval_sub(struct timeval *delta,
const struct timeval *future, const struct timeval *past)
{
- delta->tv_sec = future->tv_sec - past->tv_sec;
- delta->tv_usec = future->tv_usec - past->tv_usec;
- if (future->tv_sec < past->tv_sec || (future->tv_sec == past->tv_sec &&
- future->tv_usec < past->tv_usec)) {
- if (future->tv_usec > past->tv_usec) {
- delta->tv_usec = -MICROSECOND + delta->tv_usec;
- ++delta->tv_sec;
- }
- if (delta->tv_sec < 0)
- delta->tv_usec *= -1;
- } else if (delta->tv_usec < 0) {
- delta->tv_usec += MICROSECOND;
- --delta->tv_sec;
- }
+ struct timespec d, f, p;
+ f.tv_sec = future->tv_sec;
+ f.tv_nsec = future->tv_usec * 1000;
+ p.tv_sec = past->tv_sec;
+ p.tv_nsec = past->tv_usec * 1000;
+ HX_timespec_sub(&d, &f, &p);
+ delta->tv_sec = d.tv_sec;
+ delta->tv_usec = d.tv_nsec / 1000;
return delta;
}
#endif