summaryrefslogtreecommitdiff
path: root/src/time.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2023-02-10 15:27:06 +0100
committerJörg Frings-Fürst <debian@jff-webhosting.net>2023-02-10 15:27:06 +0100
commit7501bff8432444b7ae8e7f3d9289c0d61f3f0b64 (patch)
treebd53603f464c3747e897a8996158a0fef7b41bc3 /src/time.c
parent0f124df68d87c9073f76efeff1a901a69b1f3e13 (diff)
parent9e9336185f86bd97ff22f54e4d561c2cccccecf5 (diff)
Merge branch 'release/debian/4.10-1'debian/4.10-1
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