1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, 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/defs/time.h"
15 #include "lib/log/log.h"
20 #ifdef HAVE_SYS_TIME_H
24 /** Return the difference between start->tv_sec and end->tv_sec.
25 * Returns INT64_MAX on overflow and underflow.
28 tv_secdiff_impl(const struct timeval
*start
, const struct timeval
*end
)
30 const int64_t s
= (int64_t)start
->tv_sec
;
31 const int64_t e
= (int64_t)end
->tv_sec
;
33 /* This may not be the most efficient way of implementing this check,
34 * but it's easy to see that it's correct and doesn't overflow */
36 if (s
> 0 && e
< INT64_MIN
+ s
) {
37 /* s is positive: equivalent to e - s < INT64_MIN, but without any
40 } else if (s
< 0 && e
> INT64_MAX
+ s
) {
41 /* s is negative: equivalent to e - s > INT64_MAX, but without any
49 /** Return the number of microseconds elapsed between *start and *end.
50 * Returns LONG_MAX on overflow and underflow.
53 tv_udiff(const struct timeval
*start
, const struct timeval
*end
)
55 /* Sanity check tv_usec */
56 if (start
->tv_usec
> TOR_USEC_PER_SEC
|| start
->tv_usec
< 0) {
57 log_warn(LD_GENERAL
, "comparing times on microsecond detail with bad "
58 "start tv_usec: %"PRId64
" microseconds",
59 (int64_t)start
->tv_usec
);
63 if (end
->tv_usec
> TOR_USEC_PER_SEC
|| end
->tv_usec
< 0) {
64 log_warn(LD_GENERAL
, "comparing times on microsecond detail with bad "
65 "end tv_usec: %"PRId64
" microseconds",
66 (int64_t)end
->tv_usec
);
70 /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
73 const int64_t secdiff
= tv_secdiff_impl(start
, end
);
75 /* end->tv_usec - start->tv_usec can be up to 1 second either way */
76 if (secdiff
> (int64_t)(LONG_MAX
/1000000 - 1) ||
77 secdiff
< (int64_t)(LONG_MIN
/1000000 + 1)) {
78 log_warn(LD_GENERAL
, "comparing times on microsecond detail too far "
79 "apart: %"PRId64
" seconds", (secdiff
));
83 /* we'll never get an overflow here, because we check that both usecs are
84 * between 0 and TV_USEC_PER_SEC. */
85 udiff
= secdiff
*1000000 + ((int64_t)end
->tv_usec
- (int64_t)start
->tv_usec
);
87 /* Some compilers are smart enough to work out this is a no-op on L64 */
89 if (udiff
> (int64_t)LONG_MAX
|| udiff
< (int64_t)LONG_MIN
) {
97 /** Return the number of milliseconds elapsed between *start and *end.
98 * If the tv_usec difference is 500, rounds away from zero.
99 * Returns LONG_MAX on overflow and underflow.
102 tv_mdiff(const struct timeval
*start
, const struct timeval
*end
)
104 /* Sanity check tv_usec */
105 if (start
->tv_usec
> TOR_USEC_PER_SEC
|| start
->tv_usec
< 0) {
106 log_warn(LD_GENERAL
, "comparing times on millisecond detail with bad "
107 "start tv_usec: %"PRId64
" microseconds",
108 (int64_t)start
->tv_usec
);
112 if (end
->tv_usec
> TOR_USEC_PER_SEC
|| end
->tv_usec
< 0) {
113 log_warn(LD_GENERAL
, "comparing times on millisecond detail with bad "
114 "end tv_usec: %"PRId64
" microseconds",
115 (int64_t)end
->tv_usec
);
119 /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
122 const int64_t secdiff
= tv_secdiff_impl(start
, end
);
124 /* end->tv_usec - start->tv_usec can be up to 1 second either way, but the
125 * mdiff calculation may add another temporary second for rounding.
126 * Whether this actually causes overflow depends on the compiler's constant
127 * folding and order of operations. */
128 if (secdiff
> (int64_t)(LONG_MAX
/1000 - 2) ||
129 secdiff
< (int64_t)(LONG_MIN
/1000 + 1)) {
130 log_warn(LD_GENERAL
, "comparing times on millisecond detail too far "
131 "apart: %"PRId64
" seconds", (int64_t)secdiff
);
135 /* Subtract and round */
136 mdiff
= secdiff
*1000 +
137 /* We add a million usec here to ensure that the result is positive,
138 * so that the round-towards-zero behavior of the division will give
139 * the right result for rounding to the nearest msec. Later we subtract
140 * 1000 in order to get the correct result.
141 * We'll never get an overflow here, because we check that both usecs are
142 * between 0 and TV_USEC_PER_SEC. */
143 ((int64_t)end
->tv_usec
- (int64_t)start
->tv_usec
+ 500 + 1000000) / 1000
146 /* Some compilers are smart enough to work out this is a no-op on L64 */
148 if (mdiff
> (int64_t)LONG_MAX
|| mdiff
< (int64_t)LONG_MIN
) {
157 * Converts timeval to milliseconds.
160 tv_to_msec(const struct timeval
*tv
)
162 int64_t conv
= ((int64_t)tv
->tv_sec
)*1000L;
163 /* Round ghetto-style */
164 conv
+= ((int64_t)tv
->tv_usec
+500)/1000L;
169 * Return duration in seconds between time_t values
170 * <b>t1</b> and <b>t2</b> iff <b>t1</b> is numerically
171 * less or equal than <b>t2</b>. Otherwise, return TIME_MAX.
173 * This provides a safe way to compute difference between
174 * two UNIX timestamps (<b>t2</b> can be assumed by calling
175 * code to be later than <b>t1</b>) or two durations measured
176 * in seconds (<b>t2</b> can be assumed to be longer than
177 * <b>t1</b>). Calling code is expected to check for TIME_MAX
178 * return value and interpret that as error condition.
181 time_diff(const time_t t1
, const time_t t2
)