1 /* Work around the bug in some systems whereby gettimeofday clobbers the
2 static buffer that localtime uses for it's return value. The gettimeofday
3 function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
4 The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6.
6 Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
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 2, 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 */
26 /* Disable the definitions of these functions (from config.h)
27 so we can use the library versions here. */
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
48 static struct tm *localtime_buffer_addr;
50 /* This is a wrapper for localtime. It is used only on systems for which
51 gettimeofday clobbers the static buffer used for localtime's result.
53 On the first call, record the address of the static buffer that
54 localtime uses for its result. */
57 rpl_localtime (const time_t *timep)
59 struct tm *tm = localtime (timep);
61 if (! localtime_buffer_addr)
62 localtime_buffer_addr = tm;
67 /* Same as above, since gmtime and localtime use the same buffer. */
69 rpl_gmtime (const time_t *timep)
71 struct tm *tm = gmtime (timep);
73 if (! localtime_buffer_addr)
74 localtime_buffer_addr = tm;
79 /* This is a wrapper for gettimeofday. It is used only on systems for which
80 gettimeofday clobbers the static buffer used for localtime's result.
82 Save and restore the contents of the buffer used for localtime's result
83 around the call to gettimeofday. */
86 rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
91 if (! localtime_buffer_addr)
94 localtime_buffer_addr = localtime (&t);
97 save = *localtime_buffer_addr;
98 result = gettimeofday (tv, tz);
99 *localtime_buffer_addr = save;
104 /* This is a wrapper for tzset. It is used only on systems for which
105 tzset may clobber the static buffer used for localtime's result.
106 Save and restore the contents of the buffer used for localtime's
107 result around the call to tzset. */
113 if (! localtime_buffer_addr)
116 localtime_buffer_addr = localtime (&t);
119 save = *localtime_buffer_addr;
121 *localtime_buffer_addr = save;