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

https://bitbucket.org/freebsd/freebsd-head/ · C · 86 lines · 45 code · 16 blank · 25 comment · 9 complexity · c2b7050368fba78d3bd3a1ab13fc9f27 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2001 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: stdtime.c,v 1.19 2007/06/19 23:47:18 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stddef.h> /* NULL */
  21. #include <stdlib.h> /* NULL */
  22. #include <syslog.h>
  23. #include <sys/time.h>
  24. #include <isc/stdtime.h>
  25. #include <isc/util.h>
  26. #ifndef ISC_FIX_TV_USEC
  27. #define ISC_FIX_TV_USEC 1
  28. #endif
  29. #define US_PER_S 1000000
  30. #if ISC_FIX_TV_USEC
  31. static inline void
  32. fix_tv_usec(struct timeval *tv) {
  33. isc_boolean_t fixed = ISC_FALSE;
  34. if (tv->tv_usec < 0) {
  35. fixed = ISC_TRUE;
  36. do {
  37. tv->tv_sec -= 1;
  38. tv->tv_usec += US_PER_S;
  39. } while (tv->tv_usec < 0);
  40. } else if (tv->tv_usec >= US_PER_S) {
  41. fixed = ISC_TRUE;
  42. do {
  43. tv->tv_sec += 1;
  44. tv->tv_usec -= US_PER_S;
  45. } while (tv->tv_usec >=US_PER_S);
  46. }
  47. /*
  48. * Call syslog directly as we are called from the logging functions.
  49. */
  50. if (fixed)
  51. (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
  52. }
  53. #endif
  54. void
  55. isc_stdtime_get(isc_stdtime_t *t) {
  56. struct timeval tv;
  57. /*
  58. * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
  59. * 1970.
  60. */
  61. REQUIRE(t != NULL);
  62. RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
  63. #if ISC_FIX_TV_USEC
  64. fix_tv_usec(&tv);
  65. INSIST(tv.tv_usec >= 0);
  66. #else
  67. INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
  68. #endif
  69. *t = (unsigned int)tv.tv_sec;
  70. }