added new functions
[gnutls.git] / gl / gettimeofday.c
blob5d35060950a32e51ffd8fd5e333a3b14be2d49b4
1 /* Provide gettimeofday for systems that don't have it or for which it's broken.
3 Copyright (C) 2001-2003, 2005-2007, 2009-2012 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 /* written by Jim Meyering */
20 #include <config.h>
22 /* Specification. */
23 #include <sys/time.h>
25 #include <time.h>
27 #if HAVE_SYS_TIMEB_H
28 # include <sys/timeb.h>
29 #endif
31 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
33 /* Work around the bug in some systems whereby gettimeofday clobbers
34 the static buffer that localtime uses for its return value. The
35 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
36 this problem. The tzset replacement is necessary for at least
37 Solaris 2.5, 2.5.1, and 2.6. */
39 static struct tm tm_zero_buffer;
40 static struct tm *localtime_buffer_addr = &tm_zero_buffer;
42 # undef localtime
43 extern struct tm *localtime (time_t const *);
45 # undef gmtime
46 extern struct tm *gmtime (time_t const *);
48 /* This is a wrapper for localtime. It is used only on systems for which
49 gettimeofday clobbers the static buffer used for localtime's result.
51 On the first call, record the address of the static buffer that
52 localtime uses for its result. */
54 struct tm *
55 rpl_localtime (time_t const *timep)
57 struct tm *tm = localtime (timep);
59 if (localtime_buffer_addr == &tm_zero_buffer)
60 localtime_buffer_addr = tm;
62 return tm;
65 /* Same as above, since gmtime and localtime use the same buffer. */
66 struct tm *
67 rpl_gmtime (time_t const *timep)
69 struct tm *tm = gmtime (timep);
71 if (localtime_buffer_addr == &tm_zero_buffer)
72 localtime_buffer_addr = tm;
74 return tm;
77 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
79 #if TZSET_CLOBBERS_LOCALTIME
81 # undef tzset
82 extern void tzset (void);
84 /* This is a wrapper for tzset, for systems on which tzset may clobber
85 the static buffer used for localtime's result. */
86 void
87 rpl_tzset (void)
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;
95 #endif
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. */
102 gettimeofday (struct timeval *restrict tv, void *restrict tz)
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
112 # if defined timeval /* 'struct timeval' overridden by gnulib? */
113 # undef timeval
114 struct timeval otv;
115 int result = gettimeofday (&otv, (struct timezone *) tz);
116 if (result == 0)
118 tv->tv_sec = otv.tv_sec;
119 tv->tv_usec = otv.tv_usec;
121 # else
122 int result = gettimeofday (tv, (struct timezone *) tz);
123 # endif
125 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
126 *localtime_buffer_addr = save;
127 # endif
129 return result;
131 #else
133 # if HAVE__FTIME
135 struct _timeb timebuf;
136 _ftime (&timebuf);
137 tv->tv_sec = timebuf.time;
138 tv->tv_usec = timebuf.millitm * 1000;
140 # else
142 # if !defined OK_TO_USE_1S_CLOCK
143 # error "Only 1-second nominal clock resolution found. Is that intended?" \
144 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
145 # endif
146 tv->tv_sec = time (NULL);
147 tv->tv_usec = 0;
149 # endif
151 return 0;
153 #endif