diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-09-12 16:13:23 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-09-12 16:13:23 -0400 |
commit | b4f20ec8a634dc734b103b1773d6565c876e7a24 (patch) | |
tree | 04d4bd1b3fe2c788b4ec08c9a7545210002991ba /src/lib | |
parent | bfc847255afb093b89dd82687d796e3e3c7fcb89 (diff) | |
parent | c6a154e7b83377bffc130d757d3af3bcc124503e (diff) | |
download | tor-b4f20ec8a634dc734b103b1773d6565c876e7a24.tar.gz tor-b4f20ec8a634dc734b103b1773d6565c876e7a24.zip |
Merge remote-tracking branch 'tor-github/pr/280'
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/time/tvdiff.c | 22 | ||||
-rw-r--r-- | src/lib/time/tvdiff.h | 2 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/lib/time/tvdiff.c b/src/lib/time/tvdiff.c index 8617110e52..bc8a1166e7 100644 --- a/src/lib/time/tvdiff.c +++ b/src/lib/time/tvdiff.c @@ -165,3 +165,25 @@ tv_to_msec(const struct timeval *tv) conv += ((int64_t)tv->tv_usec+500)/1000L; return conv; } + +/** + * Return duration in seconds between time_t values + * <b>t1</b> and <b>t2</b> iff <b>t1</b> is numerically + * less or equal than <b>t2</b>. Otherwise, return TIME_MAX. + * + * This provides a safe way to compute difference between + * two UNIX timestamps (<b>t2</b> can be assumed by calling + * code to be later than <b>t1</b>) or two durations measured + * in seconds (<b>t2</b> can be assumed to be longer than + * <b>t1</b>). Calling code is expected to check for TIME_MAX + * return value and interpret that as error condition. + */ +time_t +time_diff(const time_t t1, const time_t t2) +{ + if (t1 <= t2) + return t2 - t1; + + return TIME_MAX; +} + diff --git a/src/lib/time/tvdiff.h b/src/lib/time/tvdiff.h index d78330d7d8..a15ce52ad6 100644 --- a/src/lib/time/tvdiff.h +++ b/src/lib/time/tvdiff.h @@ -18,4 +18,6 @@ long tv_udiff(const struct timeval *start, const struct timeval *end); long tv_mdiff(const struct timeval *start, const struct timeval *end); int64_t tv_to_msec(const struct timeval *tv); +time_t time_diff(const time_t from, const time_t to); + #endif |