gettimeofday.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Provide gettimeofday for systems that don't have it or for which it's broken.
  2. Copyright (C) 2001-2003, 2005-2007, 2009-2013 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #if HAVE_SYS_TIMEB_H
  19. # include <sys/timeb.h>
  20. #endif
  21. #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
  22. /* Work around the bug in some systems whereby gettimeofday clobbers
  23. the static buffer that localtime uses for its return value. The
  24. gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
  25. this problem. The tzset replacement is necessary for at least
  26. Solaris 2.5, 2.5.1, and 2.6. */
  27. static struct tm tm_zero_buffer;
  28. static struct tm *localtime_buffer_addr = &tm_zero_buffer;
  29. # undef localtime
  30. extern struct tm *localtime (time_t const *);
  31. # undef gmtime
  32. extern struct tm *gmtime (time_t const *);
  33. /* This is a wrapper for localtime. It is used only on systems for which
  34. gettimeofday clobbers the static buffer used for localtime's result.
  35. On the first call, record the address of the static buffer that
  36. localtime uses for its result. */
  37. struct tm *
  38. rpl_localtime (time_t const *timep)
  39. {
  40. struct tm *tm = localtime (timep);
  41. if (localtime_buffer_addr == &tm_zero_buffer)
  42. localtime_buffer_addr = tm;
  43. return tm;
  44. }
  45. /* Same as above, since gmtime and localtime use the same buffer. */
  46. struct tm *
  47. rpl_gmtime (time_t const *timep)
  48. {
  49. struct tm *tm = gmtime (timep);
  50. if (localtime_buffer_addr == &tm_zero_buffer)
  51. localtime_buffer_addr = tm;
  52. return tm;
  53. }
  54. #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
  55. #if TZSET_CLOBBERS_LOCALTIME
  56. # undef tzset
  57. extern void tzset (void);
  58. /* This is a wrapper for tzset, for systems on which tzset may clobber
  59. the static buffer used for localtime's result. */
  60. void
  61. rpl_tzset (void)
  62. {
  63. /* Save and restore the contents of the buffer used for localtime's
  64. result around the call to tzset. */
  65. struct tm save = *localtime_buffer_addr;
  66. tzset ();
  67. *localtime_buffer_addr = save;
  68. }
  69. #endif
  70. /* This is a wrapper for gettimeofday. It is used only on systems
  71. that lack this function, or whose implementation of this function
  72. causes problems. */
  73. int
  74. gettimeofday (struct timeval *restrict tv, void *restrict tz)
  75. {
  76. #undef gettimeofday
  77. #if HAVE_GETTIMEOFDAY
  78. # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
  79. /* Save and restore the contents of the buffer used for localtime's
  80. result around the call to gettimeofday. */
  81. struct tm save = *localtime_buffer_addr;
  82. # endif
  83. # if defined timeval /* 'struct timeval' overridden by gnulib? */
  84. # undef timeval
  85. struct timeval otv;
  86. int result = gettimeofday (&otv, (struct timezone *) tz);
  87. if (result == 0)
  88. {
  89. tv->tv_sec = otv.tv_sec;
  90. tv->tv_usec = otv.tv_usec;
  91. }
  92. # else
  93. int result = gettimeofday (tv, (struct timezone *) tz);
  94. # endif
  95. # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
  96. *localtime_buffer_addr = save;
  97. # endif
  98. return result;
  99. #else
  100. # if HAVE__FTIME
  101. struct _timeb timebuf;
  102. _ftime (&timebuf);
  103. tv->tv_sec = timebuf.time;
  104. tv->tv_usec = timebuf.millitm * 1000;
  105. # else
  106. # if !defined OK_TO_USE_1S_CLOCK
  107. # error "Only 1-second nominal clock resolution found. Is that intended?" \
  108. "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
  109. # endif
  110. tv->tv_sec = time (NULL);
  111. tv->tv_usec = 0;
  112. # endif
  113. return 0;
  114. #endif
  115. }