1 /* Provide gettimeofday for systems that don't have it, or,
2 work around the bug in some systems whereby gettimeofday clobbers the
3 static buffer that localtime uses for it's return value. The gettimeofday
4 function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
5 The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6.
7 Copyright (C) 2001, 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* written by Jim Meyering */
27 /* Disable the definitions of these functions (from config.h)
28 so we can use the library versions here. */
34 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
50 #include <sys/timeb.h>
53 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME
54 static struct tm *localtime_buffer_addr;
57 /* This is a wrapper for localtime. It is used only on systems for which
58 gettimeofday clobbers the static buffer used for localtime's result.
60 On the first call, record the address of the static buffer that
61 localtime uses for its result. */
63 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME
65 rpl_localtime (const time_t *timep)
67 struct tm *tm = localtime (timep);
69 if (! localtime_buffer_addr)
70 localtime_buffer_addr = tm;
76 /* Same as above, since gmtime and localtime use the same buffer. */
77 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME
79 rpl_gmtime (const time_t *timep)
81 struct tm *tm = gmtime (timep);
83 if (! localtime_buffer_addr)
84 localtime_buffer_addr = tm;
90 /* This is a wrapper for gettimeofday.
91 It is used only on systems that lack this function, or for whose
92 implementation of this function causes problems. */
95 rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
98 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
100 /* Save and restore the contents of the buffer used for localtime's result
101 around the call to gettimeofday. */
105 if (! localtime_buffer_addr)
108 localtime_buffer_addr = localtime (&t);
111 save = *localtime_buffer_addr;
112 result = gettimeofday (tv, tz);
113 *localtime_buffer_addr = save;
121 struct _timeb timebuf;
124 tv->tv_sec = timebuf.time;
125 tv->tv_usec = timebuf.millitm * 1000;
131 time_t t = time (NULL);
133 if (t == (time_t) -1)
144 /* This is a wrapper for tzset. It is used only on systems for which
145 tzset may clobber the static buffer used for localtime's result.
146 Save and restore the contents of the buffer used for localtime's
147 result around the call to tzset. */
148 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME
154 if (! localtime_buffer_addr)
157 localtime_buffer_addr = localtime (&t);
160 save = *localtime_buffer_addr;
162 *localtime_buffer_addr = save;