badbf643516f227ed07f4b0f3a24b0a18eba6572
[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 #undef localtime
45 extern struct tm *localtime (time_t const *);
46
47 /* This is a wrapper for localtime.  It is used only on systems for which
48    gettimeofday clobbers the static buffer used for localtime's result.
49
50    On the first call, record the address of the static buffer that
51    localtime uses for its result.  */
52
53 struct tm *
54 rpl_localtime (time_t const *timep)
55 {
56   struct tm *tm = localtime (timep);
57
58   if (localtime_buffer_addr == &tm_zero_buffer)
59     localtime_buffer_addr = tm;
60
61   return tm;
62 }
63
64 /* Same as above, since gmtime and localtime use the same buffer.  */
65 struct tm *
66 rpl_gmtime (time_t const *timep)
67 {
68 #undef gmtime
69   extern struct tm *gmtime (time_t const *);
70   struct tm *tm = gmtime (timep);
71
72   if (localtime_buffer_addr == &tm_zero_buffer)
73     localtime_buffer_addr = tm;
74
75   return tm;
76 }
77
78 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
79
80 #if TZSET_CLOBBERS_LOCALTIME
81 /* This is a wrapper for tzset, for systems on which tzset may clobber
82    the static buffer used for localtime's result.  */
83 void
84 rpl_tzset (void)
85 {
86 #undef tzset
87   extern void tzset (void);
88
89   /* Save and restore the contents of the buffer used for localtime's
90      result around the call to tzset.  */
91   struct tm save = *localtime_buffer_addr;
92   tzset ();
93   *localtime_buffer_addr = save;
94 }
95 #endif
96
97 /* This is a wrapper for gettimeofday.  It is used only on systems
98    that lack this function, or whose implementation of this function
99    causes problems.  */
100
101 int
102 rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
103 {
104 #undef gettimeofday
105 #if HAVE_GETTIMEOFDAY
106 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
107   /* Save and restore the contents of the buffer used for localtime's
108      result around the call to gettimeofday.  */
109   struct tm save = *localtime_buffer_addr;
110 # endif
111
112   int result = gettimeofday (tv, tz);
113
114 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
115   *localtime_buffer_addr = save;
116 # endif
117
118   return result;
119
120 #else
121
122 # if HAVE__FTIME
123
124   struct _timeb timebuf;
125   _ftime (&timebuf);
126   tv->tv_sec = timebuf.time;
127   tv->tv_usec = timebuf.millitm * 1000;
128
129 # else
130
131 #  if !defined OK_TO_USE_1S_CLOCK
132 #   error "Only 1-second nominal clock resolution found.  Is that intended?" \
133           "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
134 #  endif
135   tv->tv_sec = time (NULL);
136   tv->tv_usec = 0;
137
138 # endif
139
140   return 0;
141
142 #endif
143 }