1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Compute the difference between timevals, in various units.
11 #include "lib/time/tvdiff.h"
13 #include "lib/cc/compat_compiler.h"
14 #include "lib/log/log.h"
19 #ifdef HAVE_SYS_TIME_H
23 #define TOR_USEC_PER_SEC 1000000
25 /** Return the difference between start->tv_sec and end->tv_sec.
26 * Returns INT64_MAX on overflow and underflow.
29 tv_secdiff_impl(const struct timeval
*start
, const struct timeval
*end
)
31 const int64_t s
= (int64_t)start
->tv_sec
;
32 const int64_t e
= (int64_t)end
->tv_sec
;
34 /* This may not be the most efficient way of implemeting this check,
35 * but it's easy to see that it's correct and doesn't overflow */
37 if (s
> 0 && e
< INT64_MIN
+ s
) {
38 /* s is positive: equivalent to e - s < INT64_MIN, but without any
41 } else if (s
< 0 && e
> INT64_MAX
+ s
) {
42 /* s is negative: equivalent to e - s > INT64_MAX, but without any
50 /** Return the number of microseconds elapsed between *start and *end.
51 * Returns LONG_MAX on overflow and underflow.
54 tv_udiff(const struct timeval
*start
, const struct timeval
*end
)
56 /* Sanity check tv_usec */
57 if (start
->tv_usec
> TOR_USEC_PER_SEC
|| start
->tv_usec
< 0) {
58 log_warn(LD_GENERAL
, "comparing times on microsecond detail with bad "
59 "start tv_usec: %"PRId64
" microseconds",
60 (int64_t)start
->tv_usec
);
64 if (end
->tv_usec
> TOR_USEC_PER_SEC
|| end
->tv_usec
< 0) {
65 log_warn(LD_GENERAL
, "comparing times on microsecond detail with bad "
66 "end tv_usec: %"PRId64
" microseconds",
67 (int64_t)end
->tv_usec
);
71 /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
74 const int64_t secdiff
= tv_secdiff_impl(start
, end
);
76 /* end->tv_usec - start->tv_usec can be up to 1 second either way */
77 if (secdiff
> (int64_t)(LONG_MAX
/1000000 - 1) ||
78 secdiff
< (int64_t)(LONG_MIN
/1000000 + 1)) {
79 log_warn(LD_GENERAL
, "comparing times on microsecond detail too far "
80 "apart: %"PRId64
" seconds", (secdiff
));
84 /* we'll never get an overflow here, because we check that both usecs are
85 * between 0 and TV_USEC_PER_SEC. */
86 udiff
= secdiff
*1000000 + ((int64_t)end
->tv_usec
- (int64_t)start
->tv_usec
);
88 /* Some compilers are smart enough to work out this is a no-op on L64 */
90 if (udiff
> (int64_t)LONG_MAX
|| udiff
< (int64_t)LONG_MIN
) {
98 /** Return the number of milliseconds elapsed between *start and *end.
99 * If the tv_usec difference is 500, rounds away from zero.
100 * Returns LONG_MAX on overflow and underflow.
103 tv_mdiff(const struct timeval
*start
, const struct timeval
*end
)
105 /* Sanity check tv_usec */
106 if (start
->tv_usec
> TOR_USEC_PER_SEC
|| start
->tv_usec
< 0) {
107 log_warn(LD_GENERAL
, "comparing times on millisecond detail with bad "
108 "start tv_usec: %"PRId64
" microseconds",
109 (int64_t)start
->tv_usec
);
113 if (end
->tv_usec
> TOR_USEC_PER_SEC
|| end
->tv_usec
< 0) {
114 log_warn(LD_GENERAL
, "comparing times on millisecond detail with bad "
115 "end tv_usec: %"PRId64
" microseconds",
116 (int64_t)end
->tv_usec
);
120 /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
123 const int64_t secdiff
= tv_secdiff_impl(start
, end
);
125 /* end->tv_usec - start->tv_usec can be up to 1 second either way, but the
126 * mdiff calculation may add another temporary second for rounding.
127 * Whether this actually causes overflow depends on the compiler's constant
128 * folding and order of operations. */
129 if (secdiff
> (int64_t)(LONG_MAX
/1000 - 2) ||
130 secdiff
< (int64_t)(LONG_MIN
/1000 + 1)) {
131 log_warn(LD_GENERAL
, "comparing times on millisecond detail too far "
132 "apart: %"PRId64
" seconds", (int64_t)secdiff
);
136 /* Subtract and round */
137 mdiff
= secdiff
*1000 +
138 /* We add a million usec here to ensure that the result is positive,
139 * so that the round-towards-zero behavior of the division will give
140 * the right result for rounding to the nearest msec. Later we subtract
141 * 1000 in order to get the correct result.
142 * We'll never get an overflow here, because we check that both usecs are
143 * between 0 and TV_USEC_PER_SEC. */
144 ((int64_t)end
->tv_usec
- (int64_t)start
->tv_usec
+ 500 + 1000000) / 1000
147 /* Some compilers are smart enough to work out this is a no-op on L64 */
149 if (mdiff
> (int64_t)LONG_MAX
|| mdiff
< (int64_t)LONG_MIN
) {
158 * Converts timeval to milliseconds.
161 tv_to_msec(const struct timeval
*tv
)
163 int64_t conv
= ((int64_t)tv
->tv_sec
)*1000L;
164 /* Round ghetto-style */
165 conv
+= ((int64_t)tv
->tv_usec
+500)/1000L;
170 * Return duration in seconds between time_t values
171 * <b>t1</b> and <b>t2</b> iff <b>t1</b> is numerically
172 * less or equal than <b>t2</b>. Otherwise, return TIME_MAX.
174 * This provides a safe way to compute difference between
175 * two UNIX timestamps (<b>t2</b> can be assumed by calling
176 * code to be later than <b>t1</b>) or two durations measured
177 * in seconds (<b>t2</b> can be assumed to be longer than
178 * <b>t1</b>). Calling code is expected to check for TIME_MAX
179 * return value and interpret that as error condition.
182 time_diff(const time_t t1
, const time_t t2
)