New module 'xsize'.
[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    Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
6
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 2, or (at your option)
10    any later version.
11
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.
16
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* written by Jim Meyering */
22
23 #include <config.h>
24
25 /* Disable the definitions of these functions (from config.h)
26    so we can use the library versions here.  */
27 #undef gettimeofday
28 #undef gmtime
29 #undef localtime
30 #undef tzset
31
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <stdlib.h>
46
47 static struct tm *localtime_buffer_addr;
48
49 /* This is a wrapper for localtime.  It is used only on systems for which
50    gettimeofday clobbers the static buffer used for localtime's result.
51
52    On the first call, record the address of the static buffer that
53    localtime uses for its result.  */
54
55 struct tm *
56 rpl_localtime (const time_t *timep)
57 {
58   struct tm *tm = localtime (timep);
59
60   if (! localtime_buffer_addr)
61     localtime_buffer_addr = tm;
62
63   return tm;
64 }
65
66 /* Same as above, since gmtime and localtime use the same buffer.  */
67 struct tm *
68 rpl_gmtime (const time_t *timep)
69 {
70   struct tm *tm = gmtime (timep);
71
72   if (! localtime_buffer_addr)
73     localtime_buffer_addr = tm;
74
75   return tm;
76 }
77
78 /* This is a wrapper for gettimeofday.  It is used only on systems for which
79    gettimeofday clobbers the static buffer used for localtime's result.
80
81    Save and restore the contents of the buffer used for localtime's result
82    around the call to gettimeofday.  */
83
84 int
85 rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
86 {
87   struct tm save;
88   int result;
89
90   if (! localtime_buffer_addr)
91     {
92       time_t t = 0;
93       localtime_buffer_addr = localtime (&t);
94     }
95
96   save = *localtime_buffer_addr;
97   result = gettimeofday (tv, tz);
98   *localtime_buffer_addr = save;
99
100   return result;
101 }
102
103 /* This is a wrapper for tzset. It is used only on systems for which
104    tzset may clobber the static buffer used for localtime's result.
105    Save and restore the contents of the buffer used for localtime's
106    result around the call to tzset.  */
107 void
108 rpl_tzset (void)
109 {
110   struct tm save;
111
112   if (! localtime_buffer_addr)
113     {
114       time_t t = 0;
115       localtime_buffer_addr = localtime (&t);
116     }
117
118   save = *localtime_buffer_addr;
119   tzset ();
120   *localtime_buffer_addr = save;
121 }