private.h revision 1.61 1 1.53 christos /* Private header for tzdb code. */
2 1.53 christos
3 1.61 christos /* $NetBSD: private.h,v 1.61 2022/10/29 13:55:50 christos Exp $ */
4 1.2 jtc
5 1.1 jtc #ifndef PRIVATE_H
6 1.1 jtc #define PRIVATE_H
7 1.3 jtc
8 1.3 jtc /* NetBSD defaults */
9 1.3 jtc #define TM_GMTOFF tm_gmtoff
10 1.3 jtc #define TM_ZONE tm_zone
11 1.3 jtc #define STD_INSPIRED 1
12 1.3 jtc #define HAVE_LONG_DOUBLE 1
13 1.21 bjh21
14 1.21 bjh21 /* For when we build zic as a host tool. */
15 1.23 lukem #if HAVE_NBTOOL_CONFIG_H
16 1.23 lukem #include "nbtool_config.h"
17 1.21 bjh21 #endif
18 1.1 jtc
19 1.1 jtc /*
20 1.6 jtc ** This file is in the public domain, so clarified as of
21 1.25 mlelstv ** 1996-06-05 by Arthur David Olson.
22 1.6 jtc */
23 1.6 jtc
24 1.6 jtc /*
25 1.1 jtc ** This header is for use ONLY with the time conversion code.
26 1.1 jtc ** There is no guarantee that it will remain unchanged,
27 1.1 jtc ** or that it will remain at all.
28 1.1 jtc ** Do NOT copy it to any system include directory.
29 1.1 jtc ** Thank you!
30 1.1 jtc */
31 1.1 jtc
32 1.61 christos /* Define true, false and bool if they don't work out of the box. */
33 1.61 christos #if __STDC_VERSION__ < 199901
34 1.61 christos # define true 1
35 1.61 christos # define false 0
36 1.61 christos # define bool int
37 1.61 christos #elif __STDC_VERSION__ < 202311
38 1.61 christos # include <stdbool.h>
39 1.61 christos #endif
40 1.61 christos
41 1.52 christos /*
42 1.52 christos ** zdump has been made independent of the rest of the time
43 1.52 christos ** conversion package to increase confidence in the verification it provides.
44 1.52 christos ** You can use zdump to help in verifying other implementations.
45 1.52 christos ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
46 1.52 christos */
47 1.52 christos #ifndef USE_LTZ
48 1.52 christos # define USE_LTZ 1
49 1.52 christos #endif
50 1.52 christos
51 1.46 christos /* This string was in the Factory zone through version 2016f. */
52 1.25 mlelstv #define GRANDPARENTED "Local time zone must be set--see zic manual page"
53 1.25 mlelstv
54 1.1 jtc /*
55 1.1 jtc ** Defaults for preprocessor symbols.
56 1.43 christos ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
57 1.1 jtc */
58 1.1 jtc
59 1.45 christos #ifndef HAVE_DECL_ASCTIME_R
60 1.59 christos # define HAVE_DECL_ASCTIME_R 1
61 1.45 christos #endif
62 1.45 christos
63 1.51 christos #if !defined HAVE_GENERIC && defined __has_extension
64 1.51 christos # if __has_extension(c_generic_selections)
65 1.51 christos # define HAVE_GENERIC 1
66 1.51 christos # else
67 1.51 christos # define HAVE_GENERIC 0
68 1.51 christos # endif
69 1.51 christos #endif
70 1.51 christos /* _Generic is buggy in pre-4.9 GCC. */
71 1.51 christos #if !defined HAVE_GENERIC && defined __GNUC__
72 1.51 christos # define HAVE_GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
73 1.51 christos #endif
74 1.51 christos #ifndef HAVE_GENERIC
75 1.51 christos # define HAVE_GENERIC (201112 <= __STDC_VERSION__)
76 1.51 christos #endif
77 1.51 christos
78 1.61 christos #if !defined HAVE_GETRANDOM && defined __has_include
79 1.61 christos # if __has_include(<sys/random.h>)
80 1.61 christos # define HAVE_GETRANDOM true
81 1.61 christos # else
82 1.61 christos # define HAVE_GETRANDOM false
83 1.61 christos # endif
84 1.61 christos #endif
85 1.61 christos #ifndef HAVE_GETRANDOM
86 1.61 christos # define HAVE_GETRANDOM (2 < __GLIBC__ + (25 <= __GLIBC_MINOR__))
87 1.61 christos #endif
88 1.61 christos
89 1.61 christos #if !defined HAVE_GETTEXT && defined __has_include
90 1.61 christos # if __has_include(<libintl.h>)
91 1.61 christos # define HAVE_GETTEXT true
92 1.61 christos # endif
93 1.61 christos #endif
94 1.6 jtc #ifndef HAVE_GETTEXT
95 1.61 christos # define HAVE_GETTEXT false
96 1.61 christos #endif
97 1.6 jtc
98 1.17 kleink #ifndef HAVE_INCOMPATIBLE_CTIME_R
99 1.59 christos # define HAVE_INCOMPATIBLE_CTIME_R 0
100 1.51 christos #endif
101 1.17 kleink
102 1.32 christos #ifndef HAVE_LINK
103 1.59 christos # define HAVE_LINK 1
104 1.32 christos #endif /* !defined HAVE_LINK */
105 1.32 christos
106 1.57 christos #ifndef HAVE_MALLOC_ERRNO
107 1.59 christos # define HAVE_MALLOC_ERRNO 1
108 1.57 christos #endif
109 1.57 christos
110 1.45 christos #ifndef HAVE_POSIX_DECLS
111 1.59 christos # define HAVE_POSIX_DECLS 1
112 1.45 christos #endif
113 1.45 christos
114 1.61 christos #ifndef HAVE_SETENV
115 1.61 christos # define HAVE_SETENV 1
116 1.51 christos #endif
117 1.51 christos
118 1.40 christos #ifndef HAVE_STRDUP
119 1.59 christos # define HAVE_STRDUP 1
120 1.40 christos #endif
121 1.40 christos
122 1.52 christos #ifndef HAVE_STRTOLL
123 1.59 christos # define HAVE_STRTOLL 1
124 1.52 christos #endif
125 1.52 christos
126 1.12 kleink #ifndef HAVE_SYMLINK
127 1.59 christos # define HAVE_SYMLINK 1
128 1.12 kleink #endif /* !defined HAVE_SYMLINK */
129 1.6 jtc
130 1.61 christos #if !defined HAVE_SYS_STAT_H && defined __has_include
131 1.61 christos # if !__has_include(<sys/stat.h>)
132 1.61 christos # define HAVE_SYS_STAT_H false
133 1.61 christos # endif
134 1.61 christos #endif
135 1.20 kleink #ifndef HAVE_SYS_STAT_H
136 1.61 christos # define HAVE_SYS_STAT_H true
137 1.61 christos #endif
138 1.20 kleink
139 1.61 christos #if !defined HAVE_UNISTD_H && defined __has_include
140 1.61 christos # if !__has_include(<unistd.h>)
141 1.61 christos # define HAVE_UNISTD_H false
142 1.61 christos # endif
143 1.61 christos #endif
144 1.1 jtc #ifndef HAVE_UNISTD_H
145 1.61 christos # define HAVE_UNISTD_H true
146 1.61 christos #endif
147 1.5 jtc
148 1.36 christos #ifndef NETBSD_INSPIRED
149 1.36 christos # define NETBSD_INSPIRED 1
150 1.36 christos #endif
151 1.1 jtc
152 1.17 kleink #if HAVE_INCOMPATIBLE_CTIME_R
153 1.59 christos # define asctime_r _incompatible_asctime_r
154 1.59 christos # define ctime_r _incompatible_ctime_r
155 1.17 kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
156 1.17 kleink
157 1.51 christos /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
158 1.36 christos #define _GNU_SOURCE 1
159 1.47 christos /* Fix asctime_r on Solaris 11. */
160 1.36 christos #define _POSIX_PTHREAD_SEMANTICS 1
161 1.47 christos /* Enable strtoimax on pre-C99 Solaris 11. */
162 1.36 christos #define __EXTENSIONS__ 1
163 1.36 christos
164 1.61 christos /* On GNUish systems where time_t might be 32 or 64 bits, use 64.
165 1.61 christos On these platforms _FILE_OFFSET_BITS must also be 64; otherwise
166 1.61 christos setting _TIME_BITS to 64 does not work. The code does not
167 1.61 christos otherwise rely on _FILE_OFFSET_BITS being 64, since it does not
168 1.61 christos use off_t or functions like 'stat' that depend on off_t. */
169 1.51 christos #ifndef _FILE_OFFSET_BITS
170 1.51 christos # define _FILE_OFFSET_BITS 64
171 1.51 christos #endif
172 1.61 christos #if !defined _TIME_BITS && _FILE_OFFSET_BITS == 64
173 1.61 christos # define _TIME_BITS 64
174 1.61 christos #endif
175 1.51 christos
176 1.1 jtc /*
177 1.1 jtc ** Nested includes
178 1.1 jtc */
179 1.1 jtc
180 1.36 christos #ifndef __NetBSD__
181 1.56 christos /* Avoid clashes with NetBSD by renaming NetBSD's declarations.
182 1.56 christos If defining the 'timezone' variable, avoid a clash with FreeBSD's
183 1.56 christos 'timezone' function by renaming its declaration. */
184 1.36 christos #define localtime_rz sys_localtime_rz
185 1.36 christos #define mktime_z sys_mktime_z
186 1.36 christos #define posix2time_z sys_posix2time_z
187 1.36 christos #define time2posix_z sys_time2posix_z
188 1.56 christos #if defined USG_COMPAT && USG_COMPAT == 2
189 1.56 christos # define timezone sys_timezone
190 1.56 christos #endif
191 1.36 christos #define timezone_t sys_timezone_t
192 1.36 christos #define tzalloc sys_tzalloc
193 1.36 christos #define tzfree sys_tzfree
194 1.36 christos #include <time.h>
195 1.36 christos #undef localtime_rz
196 1.36 christos #undef mktime_z
197 1.36 christos #undef posix2time_z
198 1.36 christos #undef time2posix_z
199 1.56 christos #if defined USG_COMPAT && USG_COMPAT == 2
200 1.56 christos # undef timezone
201 1.56 christos #endif
202 1.36 christos #undef timezone_t
203 1.36 christos #undef tzalloc
204 1.36 christos #undef tzfree
205 1.36 christos #else
206 1.36 christos #include "time.h"
207 1.36 christos #endif
208 1.36 christos
209 1.61 christos #include <stddef.h>
210 1.50 christos #include <string.h>
211 1.50 christos #include <limits.h> /* for CHAR_BIT et al. */
212 1.50 christos #include <stdlib.h>
213 1.1 jtc
214 1.50 christos #include <errno.h>
215 1.38 christos
216 1.57 christos #ifndef EINVAL
217 1.57 christos # define EINVAL ERANGE
218 1.57 christos #endif
219 1.57 christos
220 1.60 christos #ifndef ELOOP
221 1.60 christos # define ELOOP EINVAL
222 1.60 christos #endif
223 1.38 christos #ifndef ENAMETOOLONG
224 1.38 christos # define ENAMETOOLONG EINVAL
225 1.38 christos #endif
226 1.57 christos #ifndef ENOMEM
227 1.57 christos # define ENOMEM EINVAL
228 1.57 christos #endif
229 1.45 christos #ifndef ENOTSUP
230 1.45 christos # define ENOTSUP EINVAL
231 1.45 christos #endif
232 1.38 christos #ifndef EOVERFLOW
233 1.38 christos # define EOVERFLOW EINVAL
234 1.38 christos #endif
235 1.38 christos
236 1.25 mlelstv #if HAVE_GETTEXT
237 1.59 christos # include <libintl.h>
238 1.25 mlelstv #endif /* HAVE_GETTEXT */
239 1.14 kleink
240 1.25 mlelstv #if HAVE_UNISTD_H
241 1.59 christos # include <unistd.h> /* for R_OK, and other POSIX goodness */
242 1.25 mlelstv #endif /* HAVE_UNISTD_H */
243 1.1 jtc
244 1.36 christos #ifndef HAVE_STRFTIME_L
245 1.36 christos # if _POSIX_VERSION < 200809
246 1.36 christos # define HAVE_STRFTIME_L 0
247 1.36 christos # else
248 1.36 christos # define HAVE_STRFTIME_L 1
249 1.36 christos # endif
250 1.36 christos #endif
251 1.36 christos
252 1.51 christos #ifndef USG_COMPAT
253 1.51 christos # ifndef _XOPEN_VERSION
254 1.51 christos # define USG_COMPAT 0
255 1.51 christos # else
256 1.51 christos # define USG_COMPAT 1
257 1.51 christos # endif
258 1.51 christos #endif
259 1.51 christos
260 1.51 christos #ifndef HAVE_TZNAME
261 1.51 christos # if _POSIX_VERSION < 198808 && !USG_COMPAT
262 1.51 christos # define HAVE_TZNAME 0
263 1.51 christos # else
264 1.51 christos # define HAVE_TZNAME 1
265 1.51 christos # endif
266 1.51 christos #endif
267 1.51 christos
268 1.56 christos #ifndef ALTZONE
269 1.56 christos # if defined __sun || defined _M_XENIX
270 1.56 christos # define ALTZONE 1
271 1.56 christos # else
272 1.56 christos # define ALTZONE 0
273 1.56 christos # endif
274 1.56 christos #endif
275 1.56 christos
276 1.1 jtc #ifndef R_OK
277 1.59 christos # define R_OK 4
278 1.1 jtc #endif /* !defined R_OK */
279 1.1 jtc
280 1.1 jtc /*
281 1.25 mlelstv ** Define HAVE_STDINT_H's default value here, rather than at the
282 1.47 christos ** start, since __GLIBC__ and INTMAX_MAX's values depend on
283 1.47 christos ** previously-included files. glibc 2.1 and Solaris 10 and later have
284 1.47 christos ** stdint.h, even with pre-C99 compilers.
285 1.25 mlelstv */
286 1.61 christos #if !defined HAVE_STDINT_H && defined __has_include
287 1.61 christos # define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h. */
288 1.61 christos #endif
289 1.25 mlelstv #ifndef HAVE_STDINT_H
290 1.59 christos # define HAVE_STDINT_H \
291 1.33 christos (199901 <= __STDC_VERSION__ \
292 1.59 christos || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
293 1.47 christos || __CYGWIN__ || INTMAX_MAX)
294 1.25 mlelstv #endif /* !defined HAVE_STDINT_H */
295 1.25 mlelstv
296 1.25 mlelstv #if HAVE_STDINT_H
297 1.59 christos # include <stdint.h>
298 1.25 mlelstv #endif /* !HAVE_STDINT_H */
299 1.25 mlelstv
300 1.29 christos #ifndef HAVE_INTTYPES_H
301 1.29 christos # define HAVE_INTTYPES_H HAVE_STDINT_H
302 1.29 christos #endif
303 1.29 christos #if HAVE_INTTYPES_H
304 1.29 christos # include <inttypes.h>
305 1.29 christos #endif
306 1.29 christos
307 1.36 christos /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
308 1.36 christos #ifdef __LONG_LONG_MAX__
309 1.36 christos # ifndef LLONG_MAX
310 1.36 christos # define LLONG_MAX __LONG_LONG_MAX__
311 1.36 christos # endif
312 1.36 christos # ifndef LLONG_MIN
313 1.36 christos # define LLONG_MIN (-1 - LLONG_MAX)
314 1.36 christos # endif
315 1.36 christos #endif
316 1.36 christos
317 1.25 mlelstv #ifndef INT_FAST64_MAX
318 1.36 christos # ifdef LLONG_MAX
319 1.25 mlelstv typedef long long int_fast64_t;
320 1.29 christos # define INT_FAST64_MIN LLONG_MIN
321 1.29 christos # define INT_FAST64_MAX LLONG_MAX
322 1.29 christos # else
323 1.43 christos # if LONG_MAX >> 31 < 0xffffffff
324 1.25 mlelstv Please use a compiler that supports a 64-bit integer type (or wider);
325 1.25 mlelstv you may need to compile with "-DHAVE_STDINT_H".
326 1.36 christos # endif
327 1.25 mlelstv typedef long int_fast64_t;
328 1.36 christos # define INT_FAST64_MIN LONG_MIN
329 1.36 christos # define INT_FAST64_MAX LONG_MAX
330 1.36 christos # endif
331 1.36 christos #endif
332 1.36 christos
333 1.48 christos #ifndef PRIdFAST64
334 1.36 christos # if INT_FAST64_MAX == LLONG_MAX
335 1.48 christos # define PRIdFAST64 "lld"
336 1.36 christos # else
337 1.48 christos # define PRIdFAST64 "ld"
338 1.36 christos # endif
339 1.36 christos #endif
340 1.25 mlelstv
341 1.48 christos #ifndef SCNdFAST64
342 1.48 christos # define SCNdFAST64 PRIdFAST64
343 1.48 christos #endif
344 1.48 christos
345 1.29 christos #ifndef INT_FAST32_MAX
346 1.29 christos # if INT_MAX >> 31 == 0
347 1.29 christos typedef long int_fast32_t;
348 1.36 christos # define INT_FAST32_MAX LONG_MAX
349 1.36 christos # define INT_FAST32_MIN LONG_MIN
350 1.29 christos # else
351 1.29 christos typedef int int_fast32_t;
352 1.36 christos # define INT_FAST32_MAX INT_MAX
353 1.36 christos # define INT_FAST32_MIN INT_MIN
354 1.29 christos # endif
355 1.29 christos #endif
356 1.29 christos
357 1.29 christos #ifndef INTMAX_MAX
358 1.36 christos # ifdef LLONG_MAX
359 1.29 christos typedef long long intmax_t;
360 1.52 christos # if HAVE_STRTOLL
361 1.52 christos # define strtoimax strtoll
362 1.52 christos # endif
363 1.36 christos # define INTMAX_MAX LLONG_MAX
364 1.36 christos # define INTMAX_MIN LLONG_MIN
365 1.29 christos # else
366 1.29 christos typedef long intmax_t;
367 1.30 christos # define INTMAX_MAX LONG_MAX
368 1.30 christos # define INTMAX_MIN LONG_MIN
369 1.29 christos # endif
370 1.52 christos # ifndef strtoimax
371 1.52 christos # define strtoimax strtol
372 1.52 christos # endif
373 1.29 christos #endif
374 1.29 christos
375 1.36 christos #ifndef PRIdMAX
376 1.36 christos # if INTMAX_MAX == LLONG_MAX
377 1.36 christos # define PRIdMAX "lld"
378 1.36 christos # else
379 1.36 christos # define PRIdMAX "ld"
380 1.36 christos # endif
381 1.36 christos #endif
382 1.36 christos
383 1.57 christos #ifndef UINT_FAST32_MAX
384 1.57 christos typedef unsigned long uint_fast32_t;
385 1.57 christos #endif
386 1.57 christos
387 1.43 christos #ifndef UINT_FAST64_MAX
388 1.43 christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
389 1.43 christos typedef unsigned long long uint_fast64_t;
390 1.43 christos # else
391 1.43 christos # if ULONG_MAX >> 31 >> 1 < 0xffffffff
392 1.43 christos Please use a compiler that supports a 64-bit integer type (or wider);
393 1.43 christos you may need to compile with "-DHAVE_STDINT_H".
394 1.43 christos # endif
395 1.43 christos typedef unsigned long uint_fast64_t;
396 1.43 christos # endif
397 1.43 christos #endif
398 1.43 christos
399 1.29 christos #ifndef UINTMAX_MAX
400 1.29 christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
401 1.29 christos typedef unsigned long long uintmax_t;
402 1.36 christos # else
403 1.36 christos typedef unsigned long uintmax_t;
404 1.36 christos # endif
405 1.36 christos #endif
406 1.36 christos
407 1.36 christos #ifndef PRIuMAX
408 1.36 christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
409 1.29 christos # define PRIuMAX "llu"
410 1.29 christos # else
411 1.29 christos # define PRIuMAX "lu"
412 1.29 christos # endif
413 1.29 christos #endif
414 1.29 christos
415 1.33 christos #ifndef SIZE_MAX
416 1.59 christos # define SIZE_MAX ((size_t) -1)
417 1.33 christos #endif
418 1.33 christos
419 1.52 christos #if 3 <= __GNUC__
420 1.57 christos # define ATTRIBUTE_CONST __attribute__((__const__))
421 1.57 christos # define ATTRIBUTE_MALLOC __attribute__((__malloc__))
422 1.57 christos # define ATTRIBUTE_PURE __attribute__((__pure__))
423 1.57 christos # define ATTRIBUTE_FORMAT(spec) __attribute__((__format__ spec))
424 1.27 christos #else
425 1.29 christos # define ATTRIBUTE_CONST /* empty */
426 1.52 christos # define ATTRIBUTE_MALLOC /* empty */
427 1.29 christos # define ATTRIBUTE_PURE /* empty */
428 1.30 christos # define ATTRIBUTE_FORMAT(spec) /* empty */
429 1.27 christos #endif
430 1.29 christos
431 1.29 christos #if !defined _Noreturn && __STDC_VERSION__ < 201112
432 1.29 christos # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
433 1.57 christos # define _Noreturn __attribute__((__noreturn__))
434 1.29 christos # else
435 1.29 christos # define _Noreturn
436 1.29 christos # endif
437 1.29 christos #endif
438 1.29 christos
439 1.29 christos #if __STDC_VERSION__ < 199901 && !defined restrict
440 1.29 christos # define restrict /* empty */
441 1.27 christos #endif
442 1.27 christos
443 1.1 jtc /*
444 1.25 mlelstv ** Workarounds for compilers/systems.
445 1.1 jtc */
446 1.1 jtc
447 1.48 christos #ifndef EPOCH_LOCAL
448 1.48 christos # define EPOCH_LOCAL 0
449 1.48 christos #endif
450 1.48 christos #ifndef EPOCH_OFFSET
451 1.48 christos # define EPOCH_OFFSET 0
452 1.48 christos #endif
453 1.52 christos #ifndef RESERVE_STD_EXT_IDS
454 1.52 christos # define RESERVE_STD_EXT_IDS 0
455 1.52 christos #endif
456 1.52 christos
457 1.52 christos /* If standard C identifiers with external linkage (e.g., localtime)
458 1.52 christos are reserved and are not already being renamed anyway, rename them
459 1.52 christos as if compiling with '-Dtime_tz=time_t'. */
460 1.52 christos #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
461 1.52 christos # define time_tz time_t
462 1.52 christos #endif
463 1.48 christos
464 1.1 jtc /*
465 1.29 christos ** Compile with -Dtime_tz=T to build the tz package with a private
466 1.29 christos ** time_t type equivalent to T rather than the system-supplied time_t.
467 1.29 christos ** This debugging feature can test unusual design decisions
468 1.29 christos ** (e.g., time_t wider than 'long', or unsigned time_t) even on
469 1.29 christos ** typical platforms.
470 1.29 christos */
471 1.48 christos #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
472 1.51 christos # define TZ_TIME_T 1
473 1.51 christos #else
474 1.51 christos # define TZ_TIME_T 0
475 1.51 christos #endif
476 1.51 christos
477 1.57 christos #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
478 1.57 christos static time_t sys_time(time_t *x) { return time(x); }
479 1.57 christos #endif
480 1.57 christos
481 1.51 christos #if TZ_TIME_T
482 1.29 christos
483 1.39 christos typedef time_tz tz_time_t;
484 1.39 christos
485 1.56 christos # undef asctime
486 1.56 christos # define asctime tz_asctime
487 1.56 christos # undef asctime_r
488 1.56 christos # define asctime_r tz_asctime_r
489 1.29 christos # undef ctime
490 1.29 christos # define ctime tz_ctime
491 1.29 christos # undef ctime_r
492 1.29 christos # define ctime_r tz_ctime_r
493 1.29 christos # undef difftime
494 1.29 christos # define difftime tz_difftime
495 1.29 christos # undef gmtime
496 1.29 christos # define gmtime tz_gmtime
497 1.29 christos # undef gmtime_r
498 1.29 christos # define gmtime_r tz_gmtime_r
499 1.29 christos # undef localtime
500 1.29 christos # define localtime tz_localtime
501 1.29 christos # undef localtime_r
502 1.29 christos # define localtime_r tz_localtime_r
503 1.39 christos # undef localtime_rz
504 1.39 christos # define localtime_rz tz_localtime_rz
505 1.29 christos # undef mktime
506 1.29 christos # define mktime tz_mktime
507 1.39 christos # undef mktime_z
508 1.39 christos # define mktime_z tz_mktime_z
509 1.36 christos # undef offtime
510 1.36 christos # define offtime tz_offtime
511 1.36 christos # undef posix2time
512 1.36 christos # define posix2time tz_posix2time
513 1.39 christos # undef posix2time_z
514 1.39 christos # define posix2time_z tz_posix2time_z
515 1.51 christos # undef strftime
516 1.51 christos # define strftime tz_strftime
517 1.29 christos # undef time
518 1.29 christos # define time tz_time
519 1.36 christos # undef time2posix
520 1.36 christos # define time2posix tz_time2posix
521 1.39 christos # undef time2posix_z
522 1.39 christos # define time2posix_z tz_time2posix_z
523 1.29 christos # undef time_t
524 1.29 christos # define time_t tz_time_t
525 1.36 christos # undef timegm
526 1.36 christos # define timegm tz_timegm
527 1.36 christos # undef timelocal
528 1.36 christos # define timelocal tz_timelocal
529 1.36 christos # undef timeoff
530 1.36 christos # define timeoff tz_timeoff
531 1.39 christos # undef tzalloc
532 1.39 christos # define tzalloc tz_tzalloc
533 1.39 christos # undef tzfree
534 1.39 christos # define tzfree tz_tzfree
535 1.39 christos # undef tzset
536 1.39 christos # define tzset tz_tzset
537 1.39 christos # undef tzsetwall
538 1.39 christos # define tzsetwall tz_tzsetwall
539 1.51 christos # if HAVE_STRFTIME_L
540 1.51 christos # undef strftime_l
541 1.51 christos # define strftime_l tz_strftime_l
542 1.51 christos # endif
543 1.51 christos # if HAVE_TZNAME
544 1.51 christos # undef tzname
545 1.51 christos # define tzname tz_tzname
546 1.51 christos # endif
547 1.51 christos # if USG_COMPAT
548 1.51 christos # undef daylight
549 1.51 christos # define daylight tz_daylight
550 1.51 christos # undef timezone
551 1.51 christos # define timezone tz_timezone
552 1.51 christos # endif
553 1.56 christos # if ALTZONE
554 1.51 christos # undef altzone
555 1.51 christos # define altzone tz_altzone
556 1.51 christos # endif
557 1.29 christos
558 1.56 christos char *asctime(struct tm const *);
559 1.56 christos char *asctime_r(struct tm const *restrict, char *restrict);
560 1.29 christos char *ctime(time_t const *);
561 1.29 christos char *ctime_r(time_t const *, char *);
562 1.51 christos double difftime(time_t, time_t) ATTRIBUTE_CONST;
563 1.51 christos size_t strftime(char *restrict, size_t, char const *restrict,
564 1.51 christos struct tm const *restrict);
565 1.51 christos # if HAVE_STRFTIME_L
566 1.51 christos size_t strftime_l(char *restrict, size_t, char const *restrict,
567 1.51 christos struct tm const *restrict, locale_t);
568 1.51 christos # endif
569 1.29 christos struct tm *gmtime(time_t const *);
570 1.29 christos struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
571 1.29 christos struct tm *localtime(time_t const *);
572 1.29 christos struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
573 1.29 christos time_t mktime(struct tm *);
574 1.36 christos time_t time(time_t *);
575 1.39 christos void tzset(void);
576 1.36 christos #endif
577 1.29 christos
578 1.45 christos #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
579 1.45 christos extern char *asctime_r(struct tm const *restrict, char *restrict);
580 1.36 christos #endif
581 1.36 christos
582 1.51 christos #ifndef HAVE_DECL_ENVIRON
583 1.51 christos # if defined environ || defined __USE_GNU
584 1.51 christos # define HAVE_DECL_ENVIRON 1
585 1.51 christos # else
586 1.51 christos # define HAVE_DECL_ENVIRON 0
587 1.51 christos # endif
588 1.51 christos #endif
589 1.51 christos
590 1.51 christos #if !HAVE_DECL_ENVIRON
591 1.51 christos extern char **environ;
592 1.51 christos #endif
593 1.51 christos
594 1.56 christos #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
595 1.51 christos extern char *tzname[];
596 1.56 christos #endif
597 1.56 christos #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
598 1.44 christos extern long timezone;
599 1.44 christos extern int daylight;
600 1.44 christos #endif
601 1.56 christos #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
602 1.44 christos extern long altzone;
603 1.44 christos #endif
604 1.44 christos
605 1.36 christos /*
606 1.36 christos ** The STD_INSPIRED functions are similar, but most also need
607 1.36 christos ** declarations if time_tz is defined.
608 1.36 christos */
609 1.36 christos
610 1.36 christos #ifdef STD_INSPIRED
611 1.51 christos # if TZ_TIME_T || !defined tzsetwall
612 1.36 christos void tzsetwall(void);
613 1.36 christos # endif
614 1.51 christos # if TZ_TIME_T || !defined offtime
615 1.36 christos struct tm *offtime(time_t const *, long);
616 1.36 christos # endif
617 1.51 christos # if TZ_TIME_T || !defined timegm
618 1.36 christos time_t timegm(struct tm *);
619 1.36 christos # endif
620 1.51 christos # if TZ_TIME_T || !defined timelocal
621 1.36 christos time_t timelocal(struct tm *);
622 1.36 christos # endif
623 1.51 christos # if TZ_TIME_T || !defined timeoff
624 1.36 christos time_t timeoff(struct tm *, long);
625 1.36 christos # endif
626 1.51 christos # if TZ_TIME_T || !defined time2posix
627 1.36 christos time_t time2posix(time_t);
628 1.36 christos # endif
629 1.51 christos # if TZ_TIME_T || !defined posix2time
630 1.36 christos time_t posix2time(time_t);
631 1.36 christos # endif
632 1.36 christos #endif
633 1.36 christos
634 1.36 christos /* Infer TM_ZONE on systems where this information is known, but suppress
635 1.36 christos guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
636 1.36 christos #if (defined __GLIBC__ \
637 1.36 christos || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
638 1.36 christos || (defined __APPLE__ && defined __MACH__))
639 1.36 christos # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
640 1.36 christos # define TM_GMTOFF tm_gmtoff
641 1.36 christos # endif
642 1.36 christos # if !defined TM_ZONE && !defined NO_TM_ZONE
643 1.36 christos # define TM_ZONE tm_zone
644 1.36 christos # endif
645 1.36 christos #endif
646 1.36 christos
647 1.36 christos /*
648 1.36 christos ** Define functions that are ABI compatible with NetBSD but have
649 1.36 christos ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
650 1.36 christos ** and labors under the misconception that 'const timezone_t' is a
651 1.36 christos ** pointer to a constant. This use of 'const' is ineffective, so it
652 1.36 christos ** is not done here. What we call 'struct state' NetBSD calls
653 1.36 christos ** 'struct __state', but this is a private name so it doesn't matter.
654 1.36 christos */
655 1.37 christos #ifndef __NetBSD__
656 1.36 christos #if NETBSD_INSPIRED
657 1.36 christos typedef struct state *timezone_t;
658 1.36 christos struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
659 1.36 christos struct tm *restrict);
660 1.36 christos time_t mktime_z(timezone_t restrict, struct tm *restrict);
661 1.36 christos timezone_t tzalloc(char const *);
662 1.36 christos void tzfree(timezone_t);
663 1.36 christos # ifdef STD_INSPIRED
664 1.51 christos # if TZ_TIME_T || !defined posix2time_z
665 1.36 christos time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
666 1.36 christos # endif
667 1.51 christos # if TZ_TIME_T || !defined time2posix_z
668 1.36 christos time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
669 1.36 christos # endif
670 1.36 christos # endif
671 1.29 christos #endif
672 1.37 christos #endif
673 1.29 christos
674 1.29 christos /*
675 1.1 jtc ** Finally, some convenience items.
676 1.1 jtc */
677 1.1 jtc
678 1.57 christos #define TYPE_BIT(type) (sizeof(type) * CHAR_BIT)
679 1.28 christos #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
680 1.41 christos #define TWOS_COMPLEMENT(t) (/*CONSTCOND*/(t) ~ (t) 0 < 0)
681 1.41 christos
682 1.59 christos /* Minimum and maximum of two values. Use lower case to avoid
683 1.59 christos naming clashes with standard include files. */
684 1.59 christos #define max(a, b) ((a) > (b) ? (a) : (b))
685 1.59 christos #define min(a, b) ((a) < (b) ? (a) : (b))
686 1.59 christos
687 1.41 christos /* Max and min values of the integer type T, of which only the bottom
688 1.41 christos B bits are used, and where the highest-order used bit is considered
689 1.41 christos to be a sign bit if T is signed. */
690 1.41 christos #define MAXVAL(t, b) /*LINTED*/ \
691 1.41 christos ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
692 1.41 christos - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
693 1.41 christos #define MINVAL(t, b) \
694 1.41 christos ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
695 1.41 christos
696 1.51 christos /* The extreme time values, assuming no padding. */
697 1.51 christos #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
698 1.51 christos #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
699 1.51 christos
700 1.51 christos /* The extreme time values. These are macros, not constants, so that
701 1.57 christos any portability problems occur only when compiling .c files that use
702 1.51 christos the macros, which is safer for applications that need only zdump and zic.
703 1.51 christos This implementation assumes no padding if time_t is signed and
704 1.51 christos either the compiler lacks support for _Generic or time_t is not one
705 1.51 christos of the standard signed integer types. */
706 1.51 christos #if HAVE_GENERIC
707 1.51 christos # define TIME_T_MIN \
708 1.51 christos _Generic((time_t) 0, \
709 1.51 christos signed char: SCHAR_MIN, short: SHRT_MIN, \
710 1.51 christos int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
711 1.51 christos default: TIME_T_MIN_NO_PADDING)
712 1.51 christos # define TIME_T_MAX \
713 1.51 christos (TYPE_SIGNED(time_t) \
714 1.51 christos ? _Generic((time_t) 0, \
715 1.51 christos signed char: SCHAR_MAX, short: SHRT_MAX, \
716 1.51 christos int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
717 1.51 christos default: TIME_T_MAX_NO_PADDING) \
718 1.51 christos : (time_t) -1)
719 1.48 christos #else
720 1.51 christos # define TIME_T_MIN TIME_T_MIN_NO_PADDING
721 1.51 christos # define TIME_T_MAX TIME_T_MAX_NO_PADDING
722 1.48 christos #endif
723 1.36 christos
724 1.1 jtc /*
725 1.1 jtc ** 302 / 1000 is log10(2.0) rounded up.
726 1.5 jtc ** Subtract one for the sign bit if the type is signed;
727 1.5 jtc ** add one for integer division truncation;
728 1.5 jtc ** add one more for a minus sign if the type is signed.
729 1.1 jtc */
730 1.1 jtc #define INT_STRLEN_MAXIMUM(type) \
731 1.25 mlelstv ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
732 1.25 mlelstv 1 + TYPE_SIGNED(type))
733 1.1 jtc
734 1.1 jtc /*
735 1.1 jtc ** INITIALIZE(x)
736 1.1 jtc */
737 1.1 jtc
738 1.35 christos #if defined(__GNUC__) || defined(__lint__)
739 1.34 christos # define INITIALIZE(x) ((x) = 0)
740 1.34 christos #else
741 1.34 christos # define INITIALIZE(x)
742 1.34 christos #endif
743 1.6 jtc
744 1.58 christos /* Whether memory access must strictly follow the C standard.
745 1.58 christos If 0, it's OK to read uninitialized storage so long as the value is
746 1.58 christos not relied upon. Defining it to 0 lets mktime access parts of
747 1.58 christos struct tm that might be uninitialized, as a heuristic when the
748 1.58 christos standard doesn't say what to return and when tm_gmtoff can help
749 1.58 christos mktime likely infer a better value. */
750 1.36 christos #ifndef UNINIT_TRAP
751 1.36 christos # define UNINIT_TRAP 0
752 1.36 christos #endif
753 1.36 christos
754 1.57 christos #ifdef DEBUG
755 1.61 christos # undef unreachable
756 1.61 christos # define unreachable() abort()
757 1.61 christos #elif !defined unreachable
758 1.61 christos # ifdef __has_builtin
759 1.61 christos # if __has_builtin(__builtin_unreachable)
760 1.61 christos # define unreachable() __builtin_unreachable()
761 1.61 christos # endif
762 1.61 christos # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
763 1.61 christos # define unreachable() __builtin_unreachable()
764 1.61 christos # endif
765 1.61 christos # ifndef unreachable
766 1.61 christos # define unreachable() ((void) 0)
767 1.57 christos # endif
768 1.57 christos #endif
769 1.57 christos
770 1.6 jtc /*
771 1.6 jtc ** For the benefit of GNU folk...
772 1.34 christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
773 1.6 jtc ** The default is to use gettext if available, and use MSGID otherwise.
774 1.6 jtc */
775 1.6 jtc
776 1.25 mlelstv #if HAVE_GETTEXT
777 1.6 jtc #define _(msgid) gettext(msgid)
778 1.25 mlelstv #else /* !HAVE_GETTEXT */
779 1.6 jtc #define _(msgid) msgid
780 1.25 mlelstv #endif /* !HAVE_GETTEXT */
781 1.6 jtc
782 1.34 christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
783 1.34 christos # define TZ_DOMAIN "tz"
784 1.34 christos #endif
785 1.17 kleink
786 1.17 kleink #if HAVE_INCOMPATIBLE_CTIME_R
787 1.17 kleink #undef asctime_r
788 1.17 kleink #undef ctime_r
789 1.25 mlelstv char *asctime_r(struct tm const *, char *);
790 1.25 mlelstv char *ctime_r(time_t const *, char *);
791 1.17 kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
792 1.1 jtc
793 1.50 christos /* Handy macros that are independent of tzfile implementation. */
794 1.50 christos
795 1.59 christos #ifndef SECSPERMIN
796 1.59 christos enum {
797 1.59 christos SECSPERMIN = 60,
798 1.59 christos MINSPERHOUR = 60,
799 1.59 christos SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
800 1.59 christos HOURSPERDAY = 24,
801 1.59 christos DAYSPERWEEK = 7,
802 1.59 christos DAYSPERNYEAR = 365,
803 1.59 christos DAYSPERLYEAR = DAYSPERNYEAR + 1,
804 1.59 christos MONSPERYEAR = 12,
805 1.59 christos YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
806 1.59 christos };
807 1.59 christos #endif
808 1.59 christos
809 1.50 christos #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
810 1.50 christos
811 1.57 christos #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
812 1.57 christos #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
813 1.57 christos #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
814 1.57 christos
815 1.59 christos #ifndef TM_SUNDAY
816 1.59 christos enum {
817 1.59 christos TM_SUNDAY,
818 1.59 christos TM_MONDAY,
819 1.59 christos TM_TUESDAY,
820 1.59 christos TM_WEDNESDAY,
821 1.59 christos TM_THURSDAY,
822 1.59 christos TM_FRIDAY,
823 1.59 christos TM_SATURDAY
824 1.59 christos };
825 1.59 christos #endif
826 1.59 christos
827 1.59 christos #ifndef TM_JANUARY
828 1.59 christos enum {
829 1.59 christos TM_JANUARY,
830 1.59 christos TM_FEBRUARY,
831 1.59 christos TM_MARCH,
832 1.59 christos TM_APRIL,
833 1.59 christos TM_MAY,
834 1.59 christos TM_JUNE,
835 1.59 christos TM_JULY,
836 1.59 christos TM_AUGUST,
837 1.59 christos TM_SEPTEMBER,
838 1.59 christos TM_OCTOBER,
839 1.59 christos TM_NOVEMBER,
840 1.59 christos TM_DECEMBER
841 1.59 christos };
842 1.59 christos #endif
843 1.59 christos
844 1.59 christos #ifndef TM_YEAR_BASE
845 1.59 christos enum {
846 1.59 christos TM_YEAR_BASE = 1900,
847 1.59 christos TM_WDAY_BASE = TM_MONDAY,
848 1.59 christos EPOCH_YEAR = 1970,
849 1.59 christos EPOCH_WDAY = TM_THURSDAY
850 1.59 christos };
851 1.59 christos #endif
852 1.50 christos
853 1.50 christos #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
854 1.50 christos
855 1.50 christos /*
856 1.50 christos ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
857 1.50 christos ** isleap(y) == isleap(y % 400)
858 1.50 christos ** and so
859 1.50 christos ** isleap(a + b) == isleap((a + b) % 400)
860 1.50 christos ** or
861 1.50 christos ** isleap(a + b) == isleap(a % 400 + b % 400)
862 1.50 christos ** This is true even if % means modulo rather than Fortran remainder
863 1.51 christos ** (which is allowed by C89 but not by C99 or later).
864 1.50 christos ** We use this to avoid addition overflow problems.
865 1.50 christos */
866 1.50 christos
867 1.50 christos #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
868 1.50 christos
869 1.55 christos #ifdef _LIBC
870 1.55 christos #include "reentrant.h"
871 1.54 christos extern struct __state *__lclptr;
872 1.54 christos #if defined(__LIBC12_SOURCE__)
873 1.54 christos #define tzset_unlocked __tzset_unlocked
874 1.54 christos #else
875 1.54 christos #define tzset_unlocked __tzset_unlocked50
876 1.54 christos #endif
877 1.54 christos
878 1.54 christos void tzset_unlocked(void);
879 1.54 christos #ifdef _REENTRANT
880 1.54 christos extern rwlock_t __lcl_lock;
881 1.54 christos #endif
882 1.55 christos #endif
883 1.54 christos
884 1.1 jtc #endif /* !defined PRIVATE_H */
885