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