1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Provide gettimeofday for systems that don't have it or for which it's broken.
5 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /* written by Jim Meyering */
32 # include <sys/timeb.h>
35 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
37 /* Work around the bug in some systems whereby gettimeofday clobbers
38 the static buffer that localtime uses for its return value. The
39 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
40 this problem. The tzset replacement is necessary for at least
41 Solaris 2.5, 2.5.1, and 2.6. */
43 static struct tm tm_zero_buffer;
44 static struct tm *localtime_buffer_addr = &tm_zero_buffer;
46 /* This is a wrapper for localtime. It is used only on systems for which
47 gettimeofday clobbers the static buffer used for localtime's result.
49 On the first call, record the address of the static buffer that
50 localtime uses for its result. */
53 rpl_localtime (time_t const *timep)
56 extern struct tm *localtime (time_t const *);
57 struct tm *tm = localtime (timep);
59 if (localtime_buffer_addr == &tm_zero_buffer)
60 localtime_buffer_addr = tm;
65 /* Same as above, since gmtime and localtime use the same buffer. */
67 rpl_gmtime (time_t const *timep)
70 extern struct tm *gmtime (time_t const *);
71 struct tm *tm = gmtime (timep);
73 if (localtime_buffer_addr == &tm_zero_buffer)
74 localtime_buffer_addr = tm;
79 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
81 #if TZSET_CLOBBERS_LOCALTIME
82 /* This is a wrapper for tzset, for systems on which tzset may clobber
83 the static buffer used for localtime's result. */
88 extern void tzset (void);
90 /* Save and restore the contents of the buffer used for localtime's
91 result around the call to tzset. */
92 struct tm save = *localtime_buffer_addr;
94 *localtime_buffer_addr = save;
98 /* This is a wrapper for gettimeofday. It is used only on systems
99 that lack this function, or whose implementation of this function
103 rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
106 #if HAVE_GETTIMEOFDAY
107 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
108 /* Save and restore the contents of the buffer used for localtime's
109 result around the call to gettimeofday. */
110 struct tm save = *localtime_buffer_addr;
113 int result = gettimeofday (tv, tz);
115 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
116 *localtime_buffer_addr = save;
125 struct _timeb timebuf;
127 tv->tv_sec = timebuf.time;
128 tv->tv_usec = timebuf.millitm * 1000;
132 # if !defined OK_TO_USE_1S_CLOCK
133 # error "Only 1-second nominal clock resolution found. Is that intended?" \
134 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
136 tv->tv_sec = time (NULL);