time: work with mingw + pthreads-win32 library
[gnulib.git] / lib / time.in.h
1 /* A more-standard <time.h>.
2
3    Copyright (C) 2007-2010 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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #if __GNUC__ >= 3
20 @PRAGMA_SYSTEM_HEADER@
21 #endif
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 _GL_TIME_H)
31
32 # @INCLUDE_NEXT@ @NEXT_TIME_H@
33
34 #else
35
36 # define _GL_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 #  else
59
60 #   ifdef __cplusplus
61 extern "C" {
62 #   endif
63
64 #   undef timespec
65 #   define timespec rpl_timespec
66 struct timespec
67 {
68   time_t tv_sec;
69   long int tv_nsec;
70 };
71
72 #   ifdef __cplusplus
73 }
74 #   endif
75
76 #  endif
77 # endif
78
79 /* Sleep for at least RQTP seconds unless interrupted,  If interrupted,
80    return -1 and store the remaining time into RMTP.  See
81    <http://www.opengroup.org/susv3xsh/nanosleep.html>.  */
82 # if @GNULIB_NANOSLEEP@
83 #  if @REPLACE_NANOSLEEP@
84 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
85 #    define nanosleep rpl_nanosleep
86 #   endif
87 _GL_FUNCDECL_RPL (nanosleep, int,
88                   (struct timespec const *__rqtp, struct timespec *__rmtp)
89                   _GL_ARG_NONNULL ((1)));
90 _GL_CXXALIAS_RPL (nanosleep, int,
91                   (struct timespec const *__rqtp, struct timespec *__rmtp));
92 #  else
93 #   if ! @HAVE_NANOSLEEP@
94 _GL_FUNCDECL_SYS (nanosleep, int,
95                   (struct timespec const *__rqtp, struct timespec *__rmtp)
96                   _GL_ARG_NONNULL ((1)));
97 #   endif
98 _GL_CXXALIAS_SYS (nanosleep, int,
99                   (struct timespec const *__rqtp, struct timespec *__rmtp));
100 #  endif
101 _GL_CXXALIASWARN (nanosleep);
102 # endif
103
104 /* Return the 'time_t' representation of TP and normalize TP.  */
105 # if @GNULIB_MKTIME@
106 #  if @REPLACE_MKTIME@
107 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
108 #    define mktime rpl_mktime
109 #   endif
110 _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
111 _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
112 #  else
113 _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
114 #  endif
115 _GL_CXXALIASWARN (mktime);
116 # endif
117
118 /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
119    <http://www.opengroup.org/susv3xsh/localtime_r.html> and
120    <http://www.opengroup.org/susv3xsh/gmtime_r.html>.  */
121 # if @GNULIB_TIME_R@
122 #  if @REPLACE_LOCALTIME_R@
123 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
124 #    undef localtime_r
125 #    define localtime_r rpl_localtime_r
126 #   endif
127 _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
128                                              struct tm *restrict __result)
129                                             _GL_ARG_NONNULL ((1, 2)));
130 _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
131                                              struct tm *restrict __result));
132 #  else
133 #   if ! @HAVE_LOCALTIME_R@
134 #    undef localtime_r
135 _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
136                                              struct tm *restrict __result)
137                                             _GL_ARG_NONNULL ((1, 2)));
138 #   endif
139 _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
140                                              struct tm *restrict __result));
141 #  endif
142 _GL_CXXALIASWARN (localtime_r);
143 #  if @REPLACE_LOCALTIME_R@
144 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
145 #    undef gmtime_r
146 #    define gmtime_r rpl_gmtime_r
147 #   endif
148 _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
149                                           struct tm *restrict __result)
150                                          _GL_ARG_NONNULL ((1, 2)));
151 _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
152                                           struct tm *restrict __result));
153 #  else
154 #   if ! @HAVE_LOCALTIME_R@
155 #    undef gmtime_r
156 _GL_FUNCDECL_SYS (gmtime_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 (gmtime_r, struct tm *, (time_t const *restrict __timer,
161                                           struct tm *restrict __result));
162 #  endif
163 _GL_CXXALIASWARN (gmtime_r);
164 # endif
165
166 /* Parse BUF as a time stamp, assuming FORMAT specifies its layout, and store
167    the resulting broken-down time into TM.  See
168    <http://www.opengroup.org/susv3xsh/strptime.html>.  */
169 # if @GNULIB_STRPTIME@
170 #  if ! @HAVE_STRPTIME@
171 _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
172                                      char const *restrict __format,
173                                      struct tm *restrict __tm)
174                                     _GL_ARG_NONNULL ((1, 2, 3)));
175 #  endif
176 _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
177                                      char const *restrict __format,
178                                      struct tm *restrict __tm));
179 _GL_CXXALIASWARN (strptime);
180 # endif
181
182 /* Convert TM to a time_t value, assuming UTC.  */
183 # if @GNULIB_TIMEGM@
184 #  if @REPLACE_TIMEGM@
185 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
186 #    undef timegm
187 #    define timegm rpl_timegm
188 #   endif
189 _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
190 _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
191 #  else
192 #   if ! @HAVE_TIMEGM@
193 _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
194 #   endif
195 _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
196 #  endif
197 _GL_CXXALIASWARN (timegm);
198 # endif
199
200 /* Encourage applications to avoid unsafe functions that can overrun
201    buffers when given outlandish struct tm values.  Portable
202    applications should use strftime (or even sprintf) instead.  */
203 # if defined GNULIB_POSIXCHECK
204 #  undef asctime
205 _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
206                  "better use strftime (or even sprintf) instead");
207 # endif
208 # if defined GNULIB_POSIXCHECK
209 #  undef asctime_r
210 _GL_WARN_ON_USE (asctime, "asctime_r can overrun buffers in some cases - "
211                  "better use strftime (or even sprintf) instead");
212 # endif
213 # if defined GNULIB_POSIXCHECK
214 #  undef ctime
215 _GL_WARN_ON_USE (asctime, "ctime can overrun buffers in some cases - "
216                  "better use strftime (or even sprintf) instead");
217 # endif
218 # if defined GNULIB_POSIXCHECK
219 #  undef ctime_r
220 _GL_WARN_ON_USE (asctime, "ctime_r can overrun buffers in some cases - "
221                  "better use strftime (or even sprintf) instead");
222 # endif
223
224 #endif