aboutsummaryrefslogtreecommitdiff
path: root/src/lib/time/tvdiff.c
diff options
context:
space:
mode:
authorrl1987 <rl1987@sdf.lonestar.org>2018-08-19 21:03:01 +0300
committerrl1987 <rl1987@sdf.lonestar.org>2018-08-19 21:03:01 +0300
commit5ab2110eb6b4ae9082430081cb2800018cf0dcd6 (patch)
treeacbc56ab18bdbf19362ab7341c363828c006c8df /src/lib/time/tvdiff.c
parente0b8c53f56441eb0a8d9ebc5d579b520a31291c6 (diff)
downloadtor-5ab2110eb6b4ae9082430081cb2800018cf0dcd6.tar.gz
tor-5ab2110eb6b4ae9082430081cb2800018cf0dcd6.zip
Rework predicted_ports_prediction_time_remaining() to fix CID 1438153
Diffstat (limited to 'src/lib/time/tvdiff.c')
-rw-r--r--src/lib/time/tvdiff.c22
1 files changed, 22 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;
+}
+