Remove unused function: dns_randfn_() in dns.c.
[tor.git] / src / lib / time / tvdiff.c
bloba87d0d96dce5dd3b7e12f6ea5ac74ce01738939c
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 */
6 /**
7 * \file tvdiff.c
8 * \brief Compute the difference between timevals, in various units.
9 **/
11 #include "lib/time/tvdiff.h"
13 #include "lib/cc/compat_compiler.h"
14 #include "lib/log/log.h"
16 #ifdef _WIN32
17 #include <winsock2.h>
18 #endif
19 #ifdef HAVE_SYS_TIME_H
20 #include <sys/time.h>
21 #endif
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.
28 static int64_t
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
39 * overflow */
40 return INT64_MAX;
41 } else if (s < 0 && e > INT64_MAX + s) {
42 /* s is negative: equivalent to e - s > INT64_MAX, but without any
43 * overflow */
44 return INT64_MAX;
47 return e - s;
50 /** Return the number of microseconds elapsed between *start and *end.
51 * Returns LONG_MAX on overflow and underflow.
53 long
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);
61 return LONG_MAX;
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);
68 return LONG_MAX;
71 /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
73 int64_t udiff;
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));
81 return LONG_MAX;
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 */
89 #if SIZEOF_LONG < 8
90 if (udiff > (int64_t)LONG_MAX || udiff < (int64_t)LONG_MIN) {
91 return LONG_MAX;
93 #endif
95 return (long)udiff;
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.
102 long
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);
110 return LONG_MAX;
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);
117 return LONG_MAX;
120 /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
122 int64_t mdiff;
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);
133 return LONG_MAX;
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
145 - 1000;
147 /* Some compilers are smart enough to work out this is a no-op on L64 */
148 #if SIZEOF_LONG < 8
149 if (mdiff > (int64_t)LONG_MAX || mdiff < (int64_t)LONG_MIN) {
150 return LONG_MAX;
152 #endif
154 return (long)mdiff;
158 * Converts timeval to milliseconds.
160 int64_t
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;
166 return conv;
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.
181 time_t
182 time_diff(const time_t t1, const time_t t2)
184 if (t1 <= t2)
185 return t2 - t1;
187 return TIME_MAX;