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;
47 extern struct tm *localtime (time_t const *);
50 extern struct tm *gmtime (time_t const *);
52 /* This is a wrapper for localtime. It is used only on systems for which
53 gettimeofday clobbers the static buffer used for localtime's result.
55 On the first call, record the address of the static buffer that
56 localtime uses for its result. */
59 rpl_localtime (time_t const *timep)
61 struct tm *tm = localtime (timep);
63 if (localtime_buffer_addr == &tm_zero_buffer)
64 localtime_buffer_addr = tm;
69 /* Same as above, since gmtime and localtime use the same buffer. */
71 rpl_gmtime (time_t const *timep)
73 struct tm *tm = gmtime (timep);
75 if (localtime_buffer_addr == &tm_zero_buffer)
76 localtime_buffer_addr = tm;
81 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
83 #if TZSET_CLOBBERS_LOCALTIME
86 extern void tzset (void);
88 /* This is a wrapper for tzset, for systems on which tzset may clobber
89 the static buffer used for localtime's result. */
93 /* Save and restore the contents of the buffer used for localtime's
94 result around the call to tzset. */
95 struct tm save = *localtime_buffer_addr;
97 *localtime_buffer_addr = save;
101 /* This is a wrapper for gettimeofday. It is used only on systems
102 that lack this function, or whose implementation of this function
106 rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
109 #if HAVE_GETTIMEOFDAY
110 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
111 /* Save and restore the contents of the buffer used for localtime's
112 result around the call to gettimeofday. */
113 struct tm save = *localtime_buffer_addr;
116 int result = gettimeofday (tv, tz);
118 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
119 *localtime_buffer_addr = save;
128 struct _timeb timebuf;
130 tv->tv_sec = timebuf.time;
131 tv->tv_usec = timebuf.millitm * 1000;
135 # if !defined OK_TO_USE_1S_CLOCK
136 # error "Only 1-second nominal clock resolution found. Is that intended?" \
137 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
139 tv->tv_sec = time (NULL);