diff options
author | Lennart Poettering <lennart@poettering.net> | 2012-10-16 01:02:36 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2012-10-16 01:04:32 +0200 |
commit | b1c806231b2822c66986d72b26a05ee09ac4b051 (patch) | |
tree | 85fe9b035cfc016ab214eb4500430e9cfd084188 /src/shared/util.c | |
parent | f530371f1f85a070d7d0fb5112146a43533ae00b (diff) |
util: properly handle -1 timespec/timeval
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index 15481b6b9d..1e1eb2af5f 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -143,6 +143,10 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { usec_t timespec_load(const struct timespec *ts) { assert(ts); + if (ts->tv_sec == (time_t) -1 && + ts->tv_nsec == (long) -1) + return (usec_t) -1; + return (usec_t) ts->tv_sec * USEC_PER_SEC + (usec_t) ts->tv_nsec / NSEC_PER_USEC; @@ -151,6 +155,12 @@ usec_t timespec_load(const struct timespec *ts) { struct timespec *timespec_store(struct timespec *ts, usec_t u) { assert(ts); + if (u == (usec_t) -1) { + ts->tv_sec = (time_t) -1; + ts->tv_nsec = (long) -1; + return ts; + } + ts->tv_sec = (time_t) (u / USEC_PER_SEC); ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); @@ -160,6 +170,10 @@ struct timespec *timespec_store(struct timespec *ts, usec_t u) { usec_t timeval_load(const struct timeval *tv) { assert(tv); + if (tv->tv_sec == (time_t) -1 && + tv->tv_usec == (suseconds_t) -1) + return (usec_t) -1; + return (usec_t) tv->tv_sec * USEC_PER_SEC + (usec_t) tv->tv_usec; @@ -168,6 +182,12 @@ usec_t timeval_load(const struct timeval *tv) { struct timeval *timeval_store(struct timeval *tv, usec_t u) { assert(tv); + if (u == (usec_t) -1) { + tv->tv_sec = (time_t) -1; + tv->tv_usec = (suseconds_t) -1; + return tv; + } + tv->tv_sec = (time_t) (u / USEC_PER_SEC); tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); |