gettimeofday: port recent C++ fix to Emacs
[gnulib.git] / lib / time.in.h
1 /* A more-standard <time.h>.
2
3    Copyright (C) 2007-2013 Free Software Foundation, Inc.
4
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 2, or (at your option)
8    any later version.
9
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.
14
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/>.  */
17
18 #if __GNUC__ >= 3
19 @PRAGMA_SYSTEM_HEADER@
20 #endif
21 @PRAGMA_COLUMNS@
22
23 /* Don't get in the way of glibc when it includes time.h merely to
24    declare a few standard symbols, rather than to declare all the
25    symbols.  Also, Solaris 8 <time.h> eventually includes itself
26    recursively; if that is happening, just include the system <time.h>
27    without adding our own declarations.  */
28 #if (defined __need_time_t || defined __need_clock_t \
29      || defined __need_timespec \
30      || defined _@GUARD_PREFIX@_TIME_H)
31
32 # @INCLUDE_NEXT@ @NEXT_TIME_H@
33
34 #else
35
36 # define _@GUARD_PREFIX@_TIME_H
37
38 # @INCLUDE_NEXT@ @NEXT_TIME_H@
39
40 /* NetBSD 5.0 mis-defines NULL.  */
41 # include <stddef.h>
42
43 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
44
45 /* The definition of _GL_ARG_NONNULL is copied here.  */
46
47 /* The definition of _GL_WARN_ON_USE is copied here.  */
48
49 /* Some systems don't define struct timespec (e.g., AIX 4.1, Ultrix 4.3).
50    Or they define it with the wrong member names or define it in <sys/time.h>
51    (e.g., FreeBSD circa 1997).  Stock Mingw does not define it, but the
52    pthreads-win32 library defines it in <pthread.h>.  */
53 # if ! @TIME_H_DEFINES_STRUCT_TIMESPEC@
54 #  if @SYS_TIME_H_DEFINES_STRUCT_TIMESPEC@
55 #   include <sys/time.h>
56 #  elif @PTHREAD_H_DEFINES_STRUCT_TIMESPEC@
57 #   include <pthread.h>
58 /* The pthreads-win32 <pthread.h> also defines a couple of broken macros.  */
59 #   undef asctime_r
60 #   undef ctime_r
61 #   undef gmtime_r
62 #   undef localtime_r
63 #   undef rand_r
64 #   undef strtok_r
65 #  else
66
67 #   ifdef __cplusplus
68 extern "C" {
69 #   endif
70
71 #   if !GNULIB_defined_struct_timespec
72 #    undef timespec
73 #    define timespec rpl_timespec
74 struct timespec
75 {
76   time_t tv_sec;
77   long int tv_nsec;
78 };
79 #    define GNULIB_defined_struct_timespec 1
80 #   endif
81
82 #   ifdef __cplusplus
83 }
84 #   endif
85
86 #  endif
87 # endif
88
89 # if !GNULIB_defined_struct_time_t_must_be_integral
90 /* Per http://austingroupbugs.net/view.php?id=327, POSIX requires
91    time_t to be an integer type, even though C99 permits floating
92    point.  We don't know of any implementation that uses floating
93    point, and it is much easier to write code that doesn't have to
94    worry about that corner case, so we force the issue.  */
95 struct __time_t_must_be_integral {
96   unsigned int __floating_time_t_unsupported : (time_t) 1;
97 };
98 #  define GNULIB_defined_struct_time_t_must_be_integral 1
99 # endif
100
101 /* Sleep for at least RQTP seconds unless interrupted,  If interrupted,
102    return -1 and store the remaining time into RMTP.  See
103    <http://www.opengroup.org/susv3xsh/nanosleep.html>.  */
104 # if @GNULIB_NANOSLEEP@
105 #  if @REPLACE_NANOSLEEP@
106 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
107 #    define nanosleep rpl_nanosleep
108 #   endif
109 _GL_FUNCDECL_RPL (nanosleep, int,
110                   (struct timespec const *__rqtp, struct timespec *__rmtp)
111                   _GL_ARG_NONNULL ((1)));
112 _GL_CXXALIAS_RPL (nanosleep, int,
113                   (struct timespec const *__rqtp, struct timespec *__rmtp));
114 #  else
115 #   if ! @HAVE_NANOSLEEP@
116 _GL_FUNCDECL_SYS (nanosleep, int,
117                   (struct timespec const *__rqtp, struct timespec *__rmtp)
118                   _GL_ARG_NONNULL ((1)));
119 #   endif
120 _GL_CXXALIAS_SYS (nanosleep, int,
121                   (struct timespec const *__rqtp, struct timespec *__rmtp));
122 #  endif
123 _GL_CXXALIASWARN (nanosleep);
124 # endif
125
126 /* Return the 'time_t' representation of TP and normalize TP.  */
127 # if @GNULIB_MKTIME@
128 #  if @REPLACE_MKTIME@
129 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
130 #    define mktime rpl_mktime
131 #   endif
132 _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
133 _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
134 #  else
135 _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
136 #  endif
137 _GL_CXXALIASWARN (mktime);
138 # endif
139
140 /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
141    <http://www.opengroup.org/susv3xsh/localtime_r.html> and
142    <http://www.opengroup.org/susv3xsh/gmtime_r.html>.  */
143 # if @GNULIB_TIME_R@
144 #  if @REPLACE_LOCALTIME_R@
145 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
146 #    undef localtime_r
147 #    define localtime_r rpl_localtime_r
148 #   endif
149 _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
150                                              struct tm *restrict __result)
151                                             _GL_ARG_NONNULL ((1, 2)));
152 _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
153                                              struct tm *restrict __result));
154 #  else
155 #   if ! @HAVE_DECL_LOCALTIME_R@
156 _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
157                                              struct tm *restrict __result)
158                                             _GL_ARG_NONNULL ((1, 2)));
159 #   endif
160 _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
161                                              struct tm *restrict __result));
162 #  endif
163 #  if @HAVE_DECL_LOCALTIME_R@
164 _GL_CXXALIASWARN (localtime_r);
165 #  endif
166 #  if @REPLACE_LOCALTIME_R@
167 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
168 #    undef gmtime_r
169 #    define gmtime_r rpl_gmtime_r
170 #   endif
171 _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
172                                           struct tm *restrict __result)
173                                          _GL_ARG_NONNULL ((1, 2)));
174 _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
175                                           struct tm *restrict __result));
176 #  else
177 #   if ! @HAVE_DECL_LOCALTIME_R@
178 _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
179                                           struct tm *restrict __result)
180                                          _GL_ARG_NONNULL ((1, 2)));
181 #   endif
182 _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
183                                           struct tm *restrict __result));
184 #  endif
185 #  if @HAVE_DECL_LOCALTIME_R@
186 _GL_CXXALIASWARN (gmtime_r);
187 #  endif
188 # endif
189
190 /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
191    <http://www.opengroup.org/susv3xsh/localtime.html> and
192    <http://www.opengroup.org/susv3xsh/gmtime.html>.  */
193 # if @GNULIB_GETTIMEOFDAY@
194 #  if @REPLACE_LOCALTIME@
195 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
196 #    undef localtime
197 #    define localtime rpl_localtime
198 #   endif
199 _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
200                                           _GL_ARG_NONNULL ((1)));
201 _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
202 #  else
203 _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
204 #  endif
205 _GL_CXXALIASWARN (localtime);
206 # endif
207
208 # if @GNULIB_GETTIMEOFDAY@
209 #  if @REPLACE_GMTIME@
210 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
211 #    undef gmtime
212 #    define gmtime rpl_gmtime
213 #   endif
214 _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
215                                        _GL_ARG_NONNULL ((1)));
216 _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
217 #  else
218 _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
219 #  endif
220 _GL_CXXALIASWARN (gmtime);
221 # endif
222
223 /* Parse BUF as a time stamp, assuming FORMAT specifies its layout, and store
224    the resulting broken-down time into TM.  See
225    <http://www.opengroup.org/susv3xsh/strptime.html>.  */
226 # if @GNULIB_STRPTIME@
227 #  if ! @HAVE_STRPTIME@
228 _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
229                                      char const *restrict __format,
230                                      struct tm *restrict __tm)
231                                     _GL_ARG_NONNULL ((1, 2, 3)));
232 #  endif
233 _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
234                                      char const *restrict __format,
235                                      struct tm *restrict __tm));
236 _GL_CXXALIASWARN (strptime);
237 # endif
238
239 /* Convert TM to a time_t value, assuming UTC.  */
240 # if @GNULIB_TIMEGM@
241 #  if @REPLACE_TIMEGM@
242 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
243 #    undef timegm
244 #    define timegm rpl_timegm
245 #   endif
246 _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
247 _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
248 #  else
249 #   if ! @HAVE_TIMEGM@
250 _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
251 #   endif
252 _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
253 #  endif
254 _GL_CXXALIASWARN (timegm);
255 # endif
256
257 /* Encourage applications to avoid unsafe functions that can overrun
258    buffers when given outlandish struct tm values.  Portable
259    applications should use strftime (or even sprintf) instead.  */
260 # if defined GNULIB_POSIXCHECK
261 #  undef asctime
262 _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
263                  "better use strftime (or even sprintf) instead");
264 # endif
265 # if defined GNULIB_POSIXCHECK
266 #  undef asctime_r
267 _GL_WARN_ON_USE (asctime, "asctime_r can overrun buffers in some cases - "
268                  "better use strftime (or even sprintf) instead");
269 # endif
270 # if defined GNULIB_POSIXCHECK
271 #  undef ctime
272 _GL_WARN_ON_USE (asctime, "ctime can overrun buffers in some cases - "
273                  "better use strftime (or even sprintf) instead");
274 # endif
275 # if defined GNULIB_POSIXCHECK
276 #  undef ctime_r
277 _GL_WARN_ON_USE (asctime, "ctime_r can overrun buffers in some cases - "
278                  "better use strftime (or even sprintf) instead");
279 # endif
280
281 #endif