1 /* Provide gettimeofday for systems that don't have it or for which it's broken.
3 Copyright (C) 2001-2003, 2005-2007, 2009-2015 Free Software
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 3, or (at your option)
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
19 /* written by Jim Meyering */
29 # include <sys/timeb.h>
32 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
34 /* Work around the bug in some systems whereby gettimeofday clobbers
35 the static buffer that localtime uses for its return value. The
36 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
37 this problem. The tzset replacement is necessary for at least
38 Solaris 2.5, 2.5.1, and 2.6. */
40 static struct tm tm_zero_buffer
;
41 static struct tm
*localtime_buffer_addr
= &tm_zero_buffer
;
44 extern struct tm
*localtime (time_t const *);
47 extern struct tm
*gmtime (time_t const *);
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.
52 On the first call, record the address of the static buffer that
53 localtime uses for its result. */
56 rpl_localtime (time_t const *timep
)
58 struct tm
*tm
= localtime (timep
);
60 if (localtime_buffer_addr
== &tm_zero_buffer
)
61 localtime_buffer_addr
= tm
;
66 /* Same as above, since gmtime and localtime use the same buffer. */
68 rpl_gmtime (time_t const *timep
)
70 struct tm
*tm
= gmtime (timep
);
72 if (localtime_buffer_addr
== &tm_zero_buffer
)
73 localtime_buffer_addr
= tm
;
78 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
80 #if TZSET_CLOBBERS_LOCALTIME
83 extern void tzset (void);
85 /* This is a wrapper for tzset, for systems on which tzset may clobber
86 the static buffer used for localtime's result. */
90 /* Save and restore the contents of the buffer used for localtime's
91 result around the call to tzset. */
92 struct tm save
= *localtime_buffer_addr
;
94 *localtime_buffer_addr
= save
;
98 /* This is a wrapper for gettimeofday. It is used only on systems
99 that lack this function, or whose implementation of this function
103 gettimeofday (struct timeval
*restrict tv
, void *restrict tz
)
106 #if HAVE_GETTIMEOFDAY
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
;
113 # if defined timeval /* 'struct timeval' overridden by gnulib? */
116 int result
= gettimeofday (&otv
, (struct timezone
*) tz
);
119 tv
->tv_sec
= otv
.tv_sec
;
120 tv
->tv_usec
= otv
.tv_usec
;
123 int result
= gettimeofday (tv
, (struct timezone
*) tz
);
126 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
127 *localtime_buffer_addr
= save
;
136 struct _timeb timebuf
;
138 tv
->tv_sec
= timebuf
.time
;
139 tv
->tv_usec
= timebuf
.millitm
* 1000;
143 # if !defined OK_TO_USE_1S_CLOCK
144 # error "Only 1-second nominal clock resolution found. Is that intended?" \
145 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
147 tv
->tv_sec
= time (NULL
);