* m4/gettimeofday.m4 (gl_GETTIMEOFDAY_REPLACE_LOCALTIME): Also
[gnulib.git] / lib / gettimeofday.c
1 /* Provide gettimeofday for systems that don't have it or for which it's broken.
2
3    Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* written by Jim Meyering */
21
22 #include <config.h>
23
24 /* Specification.  */
25 #include <sys/time.h>
26
27 #include <time.h>
28
29 #if HAVE_SYS_TIMEB_H
30 # include <sys/timeb.h>
31 #endif
32
33 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
34
35 /* Work around the bug in some systems whereby gettimeofday clobbers
36    the static buffer that localtime uses for its return value.  The
37    gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
38    this problem.  The tzset replacement is necessary for at least
39    Solaris 2.5, 2.5.1, and 2.6.  */
40
41 static struct tm tm_zero_buffer;
42 static struct tm *localtime_buffer_addr = &tm_zero_buffer;
43
44 /* This is a wrapper for localtime.  It is used only on systems for which
45    gettimeofday clobbers the static buffer used for localtime's result.
46
47    On the first call, record the address of the static buffer that
48    localtime uses for its result.  */
49
50 struct tm *
51 localtime (time_t const *timep)
52 {
53 #undef localtime
54   extern struct tm *localtime (time_t const *);
55   struct tm *tm = localtime (timep);
56
57   if (localtime_buffer_addr == &tm_zero_buffer)
58     localtime_buffer_addr = tm;
59
60   return tm;
61 }
62
63 /* Same as above, since gmtime and localtime use the same buffer.  */
64 struct tm *
65 gmtime (time_t const *timep)
66 {
67 #undef gmtime
68   extern struct tm *gmtime (time_t const *);
69   struct tm *tm = gmtime (timep);
70
71   if (localtime_buffer_addr == &tm_zero_buffer)
72     localtime_buffer_addr = tm;
73
74   return tm;
75 }
76
77 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
78
79 #if TZSET_CLOBBERS_LOCALTIME
80 /* This is a wrapper for tzset, for systems on which tzset may clobber
81    the static buffer used for localtime's result.  */
82 void
83 tzset (void)
84 {
85 #undef tzset
86   extern void tzset (void);
87
88   /* Save and restore the contents of the buffer used for localtime's
89      result around the call to tzset.  */
90   struct tm save = *localtime_buffer_addr;
91   tzset ();
92   *localtime_buffer_addr = save;
93 }
94 #endif
95
96 /* This is a wrapper for gettimeofday.  It is used only on systems
97    that lack this function, or whose implementation of this function
98    causes problems.  */
99
100 int
101 rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
102 {
103 #undef gettimeofday
104 #if HAVE_GETTIMEOFDAY
105   extern int gettimeofday (/* unspecified arguments */);
106
107 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
108   /* Save and restore the contents of the buffer used for localtime's
109      result around the call to gettimeofday.  */
110   struct tm save = *localtime_buffer_addr;
111 # endif
112
113   int result = gettimeofday (tv, tz);
114
115 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
116   *localtime_buffer_addr = save;
117 # endif
118
119   return result;
120
121 #else
122
123 # if HAVE__FTIME
124
125   struct _timeb timebuf;
126   _ftime (&timebuf);
127   tv->tv_sec = timebuf.time;
128   tv->tv_usec = timebuf.millitm * 1000;
129
130 # else
131
132 #  if !defined OK_TO_USE_1S_CLOCK
133 #   error "Only 1-second nominal clock resolution found.  Is that intended?" \
134           "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
135 #  endif
136   tv->tv_sec = time (NULL);
137   tv->tv_usec = 0;
138
139 # endif
140
141   return 0;
142
143 #endif
144 }