* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / gettimeofday.c
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.
5
6    Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
7
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)
11    any later version.
12
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.
17
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.  */
21
22 /* written by Jim Meyering */
23
24 #include <config.h>
25
26 /* Disable the definitions of these functions (from config.h)
27    so we can use the library versions here.  */
28 #undef gettimeofday
29 #undef gmtime
30 #undef localtime
31 #undef tzset
32
33 #include <sys/types.h>
34
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45
46 #include <stdlib.h>
47
48 static struct tm *localtime_buffer_addr;
49
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.
52
53    On the first call, record the address of the static buffer that
54    localtime uses for its result.  */
55
56 struct tm *
57 rpl_localtime (const time_t *timep)
58 {
59   struct tm *tm = localtime (timep);
60
61   if (! localtime_buffer_addr)
62     localtime_buffer_addr = tm;
63
64   return tm;
65 }
66
67 /* Same as above, since gmtime and localtime use the same buffer.  */
68 struct tm *
69 rpl_gmtime (const time_t *timep)
70 {
71   struct tm *tm = gmtime (timep);
72
73   if (! localtime_buffer_addr)
74     localtime_buffer_addr = tm;
75
76   return tm;
77 }
78
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.
81
82    Save and restore the contents of the buffer used for localtime's result
83    around the call to gettimeofday.  */
84
85 int
86 rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
87 {
88   struct tm save;
89   int result;
90
91   if (! localtime_buffer_addr)
92     {
93       time_t t = 0;
94       localtime_buffer_addr = localtime (&t);
95     }
96
97   save = *localtime_buffer_addr;
98   result = gettimeofday (tv, tz);
99   *localtime_buffer_addr = save;
100
101   return result;
102 }
103
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.  */
108 void
109 rpl_tzset (void)
110 {
111   struct tm save;
112
113   if (! localtime_buffer_addr)
114     {
115       time_t t = 0;
116       localtime_buffer_addr = localtime (&t);
117     }
118
119   save = *localtime_buffer_addr;
120   tzset ();
121   *localtime_buffer_addr = save;
122 }