/contrib/bind9/lib/isc/unix/time.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 420 lines · 257 code · 81 blank · 82 comment · 79 complexity · b1b91b3db303ed1a3aadc3ab1c305088 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2008, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <errno.h>
  21. #include <limits.h>
  22. #include <syslog.h>
  23. #include <time.h>
  24. #include <sys/time.h> /* Required for struct timeval on some platforms. */
  25. #include <isc/log.h>
  26. #include <isc/print.h>
  27. #include <isc/strerror.h>
  28. #include <isc/string.h>
  29. #include <isc/time.h>
  30. #include <isc/util.h>
  31. #define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
  32. #define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
  33. #define US_PER_S 1000000 /*%< Microseconds per second. */
  34. /*
  35. * All of the INSIST()s checks of nanoseconds < NS_PER_S are for
  36. * consistency checking of the type. In lieu of magic numbers, it
  37. * is the best we've got. The check is only performed on functions which
  38. * need an initialized type.
  39. */
  40. #ifndef ISC_FIX_TV_USEC
  41. #define ISC_FIX_TV_USEC 1
  42. #endif
  43. /*%
  44. *** Intervals
  45. ***/
  46. static isc_interval_t zero_interval = { 0, 0 };
  47. isc_interval_t *isc_interval_zero = &zero_interval;
  48. #if ISC_FIX_TV_USEC
  49. static inline void
  50. fix_tv_usec(struct timeval *tv) {
  51. isc_boolean_t fixed = ISC_FALSE;
  52. if (tv->tv_usec < 0) {
  53. fixed = ISC_TRUE;
  54. do {
  55. tv->tv_sec -= 1;
  56. tv->tv_usec += US_PER_S;
  57. } while (tv->tv_usec < 0);
  58. } else if (tv->tv_usec >= US_PER_S) {
  59. fixed = ISC_TRUE;
  60. do {
  61. tv->tv_sec += 1;
  62. tv->tv_usec -= US_PER_S;
  63. } while (tv->tv_usec >=US_PER_S);
  64. }
  65. /*
  66. * Call syslog directly as was are called from the logging functions.
  67. */
  68. if (fixed)
  69. (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
  70. }
  71. #endif
  72. void
  73. isc_interval_set(isc_interval_t *i,
  74. unsigned int seconds, unsigned int nanoseconds)
  75. {
  76. REQUIRE(i != NULL);
  77. REQUIRE(nanoseconds < NS_PER_S);
  78. i->seconds = seconds;
  79. i->nanoseconds = nanoseconds;
  80. }
  81. isc_boolean_t
  82. isc_interval_iszero(const isc_interval_t *i) {
  83. REQUIRE(i != NULL);
  84. INSIST(i->nanoseconds < NS_PER_S);
  85. if (i->seconds == 0 && i->nanoseconds == 0)
  86. return (ISC_TRUE);
  87. return (ISC_FALSE);
  88. }
  89. /***
  90. *** Absolute Times
  91. ***/
  92. static isc_time_t epoch = { 0, 0 };
  93. isc_time_t *isc_time_epoch = &epoch;
  94. void
  95. isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
  96. REQUIRE(t != NULL);
  97. REQUIRE(nanoseconds < NS_PER_S);
  98. t->seconds = seconds;
  99. t->nanoseconds = nanoseconds;
  100. }
  101. void
  102. isc_time_settoepoch(isc_time_t *t) {
  103. REQUIRE(t != NULL);
  104. t->seconds = 0;
  105. t->nanoseconds = 0;
  106. }
  107. isc_boolean_t
  108. isc_time_isepoch(const isc_time_t *t) {
  109. REQUIRE(t != NULL);
  110. INSIST(t->nanoseconds < NS_PER_S);
  111. if (t->seconds == 0 && t->nanoseconds == 0)
  112. return (ISC_TRUE);
  113. return (ISC_FALSE);
  114. }
  115. isc_result_t
  116. isc_time_now(isc_time_t *t) {
  117. struct timeval tv;
  118. char strbuf[ISC_STRERRORSIZE];
  119. REQUIRE(t != NULL);
  120. if (gettimeofday(&tv, NULL) == -1) {
  121. isc__strerror(errno, strbuf, sizeof(strbuf));
  122. UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
  123. return (ISC_R_UNEXPECTED);
  124. }
  125. /*
  126. * Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
  127. * then this test will generate warnings for platforms on which it is
  128. * unsigned. In any event, the chances of any of these problems
  129. * happening are pretty much zero, but since the libisc library ensures
  130. * certain things to be true ...
  131. */
  132. #if ISC_FIX_TV_USEC
  133. fix_tv_usec(&tv);
  134. if (tv.tv_sec < 0)
  135. return (ISC_R_UNEXPECTED);
  136. #else
  137. if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
  138. return (ISC_R_UNEXPECTED);
  139. #endif
  140. /*
  141. * Ensure the tv_sec value fits in t->seconds.
  142. */
  143. if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
  144. ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
  145. return (ISC_R_RANGE);
  146. t->seconds = tv.tv_sec;
  147. t->nanoseconds = tv.tv_usec * NS_PER_US;
  148. return (ISC_R_SUCCESS);
  149. }
  150. isc_result_t
  151. isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
  152. struct timeval tv;
  153. char strbuf[ISC_STRERRORSIZE];
  154. REQUIRE(t != NULL);
  155. REQUIRE(i != NULL);
  156. INSIST(i->nanoseconds < NS_PER_S);
  157. if (gettimeofday(&tv, NULL) == -1) {
  158. isc__strerror(errno, strbuf, sizeof(strbuf));
  159. UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
  160. return (ISC_R_UNEXPECTED);
  161. }
  162. /*
  163. * Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
  164. * then this test will generate warnings for platforms on which it is
  165. * unsigned. In any event, the chances of any of these problems
  166. * happening are pretty much zero, but since the libisc library ensures
  167. * certain things to be true ...
  168. */
  169. #if ISC_FIX_TV_USEC
  170. fix_tv_usec(&tv);
  171. if (tv.tv_sec < 0)
  172. return (ISC_R_UNEXPECTED);
  173. #else
  174. if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
  175. return (ISC_R_UNEXPECTED);
  176. #endif
  177. /*
  178. * Ensure the resulting seconds value fits in the size of an
  179. * unsigned int. (It is written this way as a slight optimization;
  180. * note that even if both values == INT_MAX, then when added
  181. * and getting another 1 added below the result is UINT_MAX.)
  182. */
  183. if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
  184. ((long long)tv.tv_sec + i->seconds > UINT_MAX))
  185. return (ISC_R_RANGE);
  186. t->seconds = tv.tv_sec + i->seconds;
  187. t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
  188. if (t->nanoseconds >= NS_PER_S) {
  189. t->seconds++;
  190. t->nanoseconds -= NS_PER_S;
  191. }
  192. return (ISC_R_SUCCESS);
  193. }
  194. int
  195. isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
  196. REQUIRE(t1 != NULL && t2 != NULL);
  197. INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
  198. if (t1->seconds < t2->seconds)
  199. return (-1);
  200. if (t1->seconds > t2->seconds)
  201. return (1);
  202. if (t1->nanoseconds < t2->nanoseconds)
  203. return (-1);
  204. if (t1->nanoseconds > t2->nanoseconds)
  205. return (1);
  206. return (0);
  207. }
  208. isc_result_t
  209. isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
  210. {
  211. REQUIRE(t != NULL && i != NULL && result != NULL);
  212. INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
  213. /*
  214. * Ensure the resulting seconds value fits in the size of an
  215. * unsigned int. (It is written this way as a slight optimization;
  216. * note that even if both values == INT_MAX, then when added
  217. * and getting another 1 added below the result is UINT_MAX.)
  218. */
  219. if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
  220. ((long long)t->seconds + i->seconds > UINT_MAX))
  221. return (ISC_R_RANGE);
  222. result->seconds = t->seconds + i->seconds;
  223. result->nanoseconds = t->nanoseconds + i->nanoseconds;
  224. if (result->nanoseconds >= NS_PER_S) {
  225. result->seconds++;
  226. result->nanoseconds -= NS_PER_S;
  227. }
  228. return (ISC_R_SUCCESS);
  229. }
  230. isc_result_t
  231. isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
  232. isc_time_t *result)
  233. {
  234. REQUIRE(t != NULL && i != NULL && result != NULL);
  235. INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
  236. if ((unsigned int)t->seconds < i->seconds ||
  237. ((unsigned int)t->seconds == i->seconds &&
  238. t->nanoseconds < i->nanoseconds))
  239. return (ISC_R_RANGE);
  240. result->seconds = t->seconds - i->seconds;
  241. if (t->nanoseconds >= i->nanoseconds)
  242. result->nanoseconds = t->nanoseconds - i->nanoseconds;
  243. else {
  244. result->nanoseconds = NS_PER_S - i->nanoseconds +
  245. t->nanoseconds;
  246. result->seconds--;
  247. }
  248. return (ISC_R_SUCCESS);
  249. }
  250. isc_uint64_t
  251. isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
  252. isc_uint64_t i1, i2, i3;
  253. REQUIRE(t1 != NULL && t2 != NULL);
  254. INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
  255. i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
  256. i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
  257. if (i1 <= i2)
  258. return (0);
  259. i3 = i1 - i2;
  260. /*
  261. * Convert to microseconds.
  262. */
  263. i3 /= NS_PER_US;
  264. return (i3);
  265. }
  266. isc_uint32_t
  267. isc_time_seconds(const isc_time_t *t) {
  268. REQUIRE(t != NULL);
  269. INSIST(t->nanoseconds < NS_PER_S);
  270. return ((isc_uint32_t)t->seconds);
  271. }
  272. isc_result_t
  273. isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
  274. time_t seconds;
  275. REQUIRE(t != NULL);
  276. INSIST(t->nanoseconds < NS_PER_S);
  277. /*
  278. * Ensure that the number of seconds represented by t->seconds
  279. * can be represented by a time_t. Since t->seconds is an unsigned
  280. * int and since time_t is mostly opaque, this is trickier than
  281. * it seems. (This standardized opaqueness of time_t is *very*
  282. * frustrating; time_t is not even limited to being an integral
  283. * type.)
  284. *
  285. * The mission, then, is to avoid generating any kind of warning
  286. * about "signed versus unsigned" while trying to determine if the
  287. * the unsigned int t->seconds is out range for tv_sec, which is
  288. * pretty much only true if time_t is a signed integer of the same
  289. * size as the return value of isc_time_seconds.
  290. *
  291. * If the paradox in the if clause below is true, t->seconds is out
  292. * of range for time_t.
  293. */
  294. seconds = (time_t)t->seconds;
  295. INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
  296. INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
  297. if (t->seconds > (~0U>>1) && seconds <= (time_t)(~0U>>1))
  298. return (ISC_R_RANGE);
  299. *secondsp = seconds;
  300. return (ISC_R_SUCCESS);
  301. }
  302. isc_uint32_t
  303. isc_time_nanoseconds(const isc_time_t *t) {
  304. REQUIRE(t != NULL);
  305. ENSURE(t->nanoseconds < NS_PER_S);
  306. return ((isc_uint32_t)t->nanoseconds);
  307. }
  308. void
  309. isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
  310. time_t now;
  311. unsigned int flen;
  312. REQUIRE(len > 0);
  313. now = (time_t) t->seconds;
  314. flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
  315. INSIST(flen < len);
  316. if (flen != 0)
  317. snprintf(buf + flen, len - flen,
  318. ".%03u", t->nanoseconds / 1000000);
  319. else
  320. snprintf(buf, len, "99-Bad-9999 99:99:99.999");
  321. }
  322. void
  323. isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
  324. time_t now;
  325. unsigned int flen;
  326. REQUIRE(len > 0);
  327. now = (time_t)t->seconds;
  328. flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
  329. INSIST(flen < len);
  330. }
  331. void
  332. isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
  333. time_t now;
  334. unsigned int flen;
  335. REQUIRE(len > 0);
  336. now = (time_t)t->seconds;
  337. flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
  338. INSIST(flen < len);
  339. }