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-2003, 2005-2007, 2009-2010 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* written by Jim Meyering */
31 # include <sys/timeb.h>
34 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
36 /* Work around the bug in some systems whereby gettimeofday clobbers
37 the static buffer that localtime uses for its return value. The
38 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
39 this problem. The tzset replacement is necessary for at least
40 Solaris 2.5, 2.5.1, and 2.6. */
42 static struct tm tm_zero_buffer;
43 static struct tm *localtime_buffer_addr = &tm_zero_buffer;
46 extern struct tm *localtime (time_t const *);
49 extern struct tm *gmtime (time_t const *);
51 /* This is a wrapper for localtime. It is used only on systems for which
52 gettimeofday clobbers the static buffer used for localtime's result.
54 On the first call, record the address of the static buffer that
55 localtime uses for its result. */
58 rpl_localtime (time_t const *timep)
60 struct tm *tm = localtime (timep);
62 if (localtime_buffer_addr == &tm_zero_buffer)
63 localtime_buffer_addr = tm;
68 /* Same as above, since gmtime and localtime use the same buffer. */
70 rpl_gmtime (time_t const *timep)
72 struct tm *tm = gmtime (timep);
74 if (localtime_buffer_addr == &tm_zero_buffer)
75 localtime_buffer_addr = tm;
80 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
82 #if TZSET_CLOBBERS_LOCALTIME
85 extern void tzset (void);
87 /* This is a wrapper for tzset, for systems on which tzset may clobber
88 the static buffer used for localtime's result. */
92 /* Save and restore the contents of the buffer used for localtime's
93 result around the call to tzset. */
94 struct tm save = *localtime_buffer_addr;
96 *localtime_buffer_addr = save;
100 /* This is a wrapper for gettimeofday. It is used only on systems
101 that lack this function, or whose implementation of this function
105 gettimeofday (struct timeval *restrict tv, void *restrict tz)
108 #if HAVE_GETTIMEOFDAY
109 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
110 /* Save and restore the contents of the buffer used for localtime's
111 result around the call to gettimeofday. */
112 struct tm save = *localtime_buffer_addr;
115 int result = gettimeofday (tv, (struct timezone *) tz);
117 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
118 *localtime_buffer_addr = save;
127 struct _timeb timebuf;
129 tv->tv_sec = timebuf.time;
130 tv->tv_usec = timebuf.millitm * 1000;
134 # if !defined OK_TO_USE_1S_CLOCK
135 # error "Only 1-second nominal clock resolution found. Is that intended?" \
136 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
138 tv->tv_sec = time (NULL);