private.h revision 1.53 1 1.53 christos /* Private header for tzdb code. */
2 1.53 christos
3 1.53 christos /* $NetBSD: private.h,v 1.53 2018/10/19 23:05:35 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.45 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.6 jtc #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.17 kleink #define HAVE_INCOMPATIBLE_CTIME_R 0
75 1.51 christos #endif
76 1.17 kleink
77 1.32 christos #ifndef HAVE_LINK
78 1.32 christos #define HAVE_LINK 1
79 1.32 christos #endif /* !defined HAVE_LINK */
80 1.32 christos
81 1.45 christos #ifndef HAVE_POSIX_DECLS
82 1.45 christos #define HAVE_POSIX_DECLS 1
83 1.45 christos #endif
84 1.45 christos
85 1.51 christos #ifndef HAVE_STDBOOL_H
86 1.51 christos #define HAVE_STDBOOL_H (199901 <= __STDC_VERSION__)
87 1.51 christos #endif
88 1.51 christos
89 1.40 christos #ifndef HAVE_STRDUP
90 1.40 christos #define HAVE_STRDUP 1
91 1.40 christos #endif
92 1.40 christos
93 1.52 christos #ifndef HAVE_STRTOLL
94 1.52 christos #define HAVE_STRTOLL 1
95 1.52 christos #endif
96 1.52 christos
97 1.12 kleink #ifndef HAVE_SYMLINK
98 1.12 kleink #define HAVE_SYMLINK 1
99 1.12 kleink #endif /* !defined HAVE_SYMLINK */
100 1.6 jtc
101 1.20 kleink #ifndef HAVE_SYS_STAT_H
102 1.20 kleink #define HAVE_SYS_STAT_H 1
103 1.20 kleink #endif /* !defined HAVE_SYS_STAT_H */
104 1.20 kleink
105 1.14 kleink #ifndef HAVE_SYS_WAIT_H
106 1.14 kleink #define HAVE_SYS_WAIT_H 1
107 1.14 kleink #endif /* !defined HAVE_SYS_WAIT_H */
108 1.14 kleink
109 1.1 jtc #ifndef HAVE_UNISTD_H
110 1.1 jtc #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.43 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.17 kleink #define asctime_r _incompatible_asctime_r
123 1.17 kleink #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.36 christos /* Avoid clashes with NetBSD by renaming NetBSD's declarations. */
149 1.36 christos #define localtime_rz sys_localtime_rz
150 1.36 christos #define mktime_z sys_mktime_z
151 1.36 christos #define posix2time_z sys_posix2time_z
152 1.36 christos #define time2posix_z sys_time2posix_z
153 1.36 christos #define timezone_t sys_timezone_t
154 1.36 christos #define tzalloc sys_tzalloc
155 1.36 christos #define tzfree sys_tzfree
156 1.36 christos #include <time.h>
157 1.36 christos #undef localtime_rz
158 1.36 christos #undef mktime_z
159 1.36 christos #undef posix2time_z
160 1.36 christos #undef time2posix_z
161 1.36 christos #undef timezone_t
162 1.36 christos #undef tzalloc
163 1.36 christos #undef tzfree
164 1.36 christos #else
165 1.36 christos #include "time.h"
166 1.36 christos #endif
167 1.36 christos
168 1.50 christos #include <sys/types.h> /* for time_t */
169 1.50 christos #include <string.h>
170 1.50 christos #include <limits.h> /* for CHAR_BIT et al. */
171 1.50 christos #include <stdlib.h>
172 1.1 jtc
173 1.50 christos #include <errno.h>
174 1.38 christos
175 1.38 christos #ifndef ENAMETOOLONG
176 1.38 christos # define ENAMETOOLONG EINVAL
177 1.38 christos #endif
178 1.45 christos #ifndef ENOTSUP
179 1.45 christos # define ENOTSUP EINVAL
180 1.45 christos #endif
181 1.38 christos #ifndef EOVERFLOW
182 1.38 christos # define EOVERFLOW EINVAL
183 1.38 christos #endif
184 1.38 christos
185 1.25 mlelstv #if HAVE_GETTEXT
186 1.50 christos #include <libintl.h>
187 1.25 mlelstv #endif /* HAVE_GETTEXT */
188 1.14 kleink
189 1.25 mlelstv #if HAVE_UNISTD_H
190 1.51 christos #include <unistd.h> /* for R_OK, and other POSIX goodness */
191 1.25 mlelstv #endif /* HAVE_UNISTD_H */
192 1.1 jtc
193 1.36 christos #ifndef HAVE_STRFTIME_L
194 1.36 christos # if _POSIX_VERSION < 200809
195 1.36 christos # define HAVE_STRFTIME_L 0
196 1.36 christos # else
197 1.36 christos # define HAVE_STRFTIME_L 1
198 1.36 christos # endif
199 1.36 christos #endif
200 1.36 christos
201 1.51 christos #ifndef USG_COMPAT
202 1.51 christos # ifndef _XOPEN_VERSION
203 1.51 christos # define USG_COMPAT 0
204 1.51 christos # else
205 1.51 christos # define USG_COMPAT 1
206 1.51 christos # endif
207 1.51 christos #endif
208 1.51 christos
209 1.51 christos #ifndef HAVE_TZNAME
210 1.51 christos # if _POSIX_VERSION < 198808 && !USG_COMPAT
211 1.51 christos # define HAVE_TZNAME 0
212 1.51 christos # else
213 1.51 christos # define HAVE_TZNAME 1
214 1.51 christos # endif
215 1.51 christos #endif
216 1.51 christos
217 1.1 jtc #ifndef R_OK
218 1.1 jtc #define R_OK 4
219 1.1 jtc #endif /* !defined R_OK */
220 1.1 jtc
221 1.25 mlelstv /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
222 1.4 jtc #define is_digit(c) ((unsigned)(c) - '0' <= 9)
223 1.4 jtc
224 1.1 jtc /*
225 1.25 mlelstv ** Define HAVE_STDINT_H's default value here, rather than at the
226 1.47 christos ** start, since __GLIBC__ and INTMAX_MAX's values depend on
227 1.47 christos ** previously-included files. glibc 2.1 and Solaris 10 and later have
228 1.47 christos ** stdint.h, even with pre-C99 compilers.
229 1.25 mlelstv */
230 1.25 mlelstv #ifndef HAVE_STDINT_H
231 1.25 mlelstv #define HAVE_STDINT_H \
232 1.33 christos (199901 <= __STDC_VERSION__ \
233 1.33 christos || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
234 1.47 christos || __CYGWIN__ || INTMAX_MAX)
235 1.25 mlelstv #endif /* !defined HAVE_STDINT_H */
236 1.25 mlelstv
237 1.25 mlelstv #if HAVE_STDINT_H
238 1.50 christos #include <stdint.h>
239 1.25 mlelstv #endif /* !HAVE_STDINT_H */
240 1.25 mlelstv
241 1.29 christos #ifndef HAVE_INTTYPES_H
242 1.29 christos # define HAVE_INTTYPES_H HAVE_STDINT_H
243 1.29 christos #endif
244 1.29 christos #if HAVE_INTTYPES_H
245 1.29 christos # include <inttypes.h>
246 1.29 christos #endif
247 1.29 christos
248 1.36 christos /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
249 1.36 christos #ifdef __LONG_LONG_MAX__
250 1.36 christos # ifndef LLONG_MAX
251 1.36 christos # define LLONG_MAX __LONG_LONG_MAX__
252 1.36 christos # endif
253 1.36 christos # ifndef LLONG_MIN
254 1.36 christos # define LLONG_MIN (-1 - LLONG_MAX)
255 1.36 christos # endif
256 1.36 christos #endif
257 1.36 christos
258 1.25 mlelstv #ifndef INT_FAST64_MAX
259 1.36 christos # ifdef LLONG_MAX
260 1.25 mlelstv typedef long long int_fast64_t;
261 1.29 christos # define INT_FAST64_MIN LLONG_MIN
262 1.29 christos # define INT_FAST64_MAX LLONG_MAX
263 1.29 christos # else
264 1.43 christos # if LONG_MAX >> 31 < 0xffffffff
265 1.25 mlelstv Please use a compiler that supports a 64-bit integer type (or wider);
266 1.25 mlelstv you may need to compile with "-DHAVE_STDINT_H".
267 1.36 christos # endif
268 1.25 mlelstv typedef long int_fast64_t;
269 1.36 christos # define INT_FAST64_MIN LONG_MIN
270 1.36 christos # define INT_FAST64_MAX LONG_MAX
271 1.36 christos # endif
272 1.36 christos #endif
273 1.36 christos
274 1.48 christos #ifndef PRIdFAST64
275 1.36 christos # if INT_FAST64_MAX == LLONG_MAX
276 1.48 christos # define PRIdFAST64 "lld"
277 1.36 christos # else
278 1.48 christos # define PRIdFAST64 "ld"
279 1.36 christos # endif
280 1.36 christos #endif
281 1.25 mlelstv
282 1.48 christos #ifndef SCNdFAST64
283 1.48 christos # define SCNdFAST64 PRIdFAST64
284 1.48 christos #endif
285 1.48 christos
286 1.29 christos #ifndef INT_FAST32_MAX
287 1.29 christos # if INT_MAX >> 31 == 0
288 1.29 christos typedef long int_fast32_t;
289 1.36 christos # define INT_FAST32_MAX LONG_MAX
290 1.36 christos # define INT_FAST32_MIN LONG_MIN
291 1.29 christos # else
292 1.29 christos typedef int int_fast32_t;
293 1.36 christos # define INT_FAST32_MAX INT_MAX
294 1.36 christos # define INT_FAST32_MIN INT_MIN
295 1.29 christos # endif
296 1.29 christos #endif
297 1.29 christos
298 1.29 christos #ifndef INTMAX_MAX
299 1.36 christos # ifdef LLONG_MAX
300 1.29 christos typedef long long intmax_t;
301 1.52 christos # if HAVE_STRTOLL
302 1.52 christos # define strtoimax strtoll
303 1.52 christos # endif
304 1.36 christos # define INTMAX_MAX LLONG_MAX
305 1.36 christos # define INTMAX_MIN LLONG_MIN
306 1.29 christos # else
307 1.29 christos typedef long intmax_t;
308 1.30 christos # define INTMAX_MAX LONG_MAX
309 1.30 christos # define INTMAX_MIN LONG_MIN
310 1.29 christos # endif
311 1.52 christos # ifndef strtoimax
312 1.52 christos # define strtoimax strtol
313 1.52 christos # endif
314 1.29 christos #endif
315 1.29 christos
316 1.36 christos #ifndef PRIdMAX
317 1.36 christos # if INTMAX_MAX == LLONG_MAX
318 1.36 christos # define PRIdMAX "lld"
319 1.36 christos # else
320 1.36 christos # define PRIdMAX "ld"
321 1.36 christos # endif
322 1.36 christos #endif
323 1.36 christos
324 1.43 christos #ifndef UINT_FAST64_MAX
325 1.43 christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
326 1.43 christos typedef unsigned long long uint_fast64_t;
327 1.43 christos # else
328 1.43 christos # if ULONG_MAX >> 31 >> 1 < 0xffffffff
329 1.43 christos Please use a compiler that supports a 64-bit integer type (or wider);
330 1.43 christos you may need to compile with "-DHAVE_STDINT_H".
331 1.43 christos # endif
332 1.43 christos typedef unsigned long uint_fast64_t;
333 1.43 christos # endif
334 1.43 christos #endif
335 1.43 christos
336 1.29 christos #ifndef UINTMAX_MAX
337 1.29 christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
338 1.29 christos typedef unsigned long long uintmax_t;
339 1.36 christos # else
340 1.36 christos typedef unsigned long uintmax_t;
341 1.36 christos # endif
342 1.36 christos #endif
343 1.36 christos
344 1.36 christos #ifndef PRIuMAX
345 1.36 christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
346 1.29 christos # define PRIuMAX "llu"
347 1.29 christos # else
348 1.29 christos # define PRIuMAX "lu"
349 1.29 christos # endif
350 1.29 christos #endif
351 1.29 christos
352 1.25 mlelstv #ifndef INT32_MAX
353 1.25 mlelstv #define INT32_MAX 0x7fffffff
354 1.25 mlelstv #endif /* !defined INT32_MAX */
355 1.25 mlelstv #ifndef INT32_MIN
356 1.25 mlelstv #define INT32_MIN (-1 - INT32_MAX)
357 1.25 mlelstv #endif /* !defined INT32_MIN */
358 1.1 jtc
359 1.33 christos #ifndef SIZE_MAX
360 1.33 christos #define SIZE_MAX ((size_t) -1)
361 1.33 christos #endif
362 1.33 christos
363 1.52 christos #if 3 <= __GNUC__
364 1.29 christos # define ATTRIBUTE_CONST __attribute__ ((__const__))
365 1.52 christos # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
366 1.29 christos # define ATTRIBUTE_PURE __attribute__ ((__pure__))
367 1.30 christos # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
368 1.27 christos #else
369 1.29 christos # define ATTRIBUTE_CONST /* empty */
370 1.52 christos # define ATTRIBUTE_MALLOC /* empty */
371 1.29 christos # define ATTRIBUTE_PURE /* empty */
372 1.30 christos # define ATTRIBUTE_FORMAT(spec) /* empty */
373 1.27 christos #endif
374 1.29 christos
375 1.29 christos #if !defined _Noreturn && __STDC_VERSION__ < 201112
376 1.29 christos # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
377 1.29 christos # define _Noreturn __attribute__ ((__noreturn__))
378 1.29 christos # else
379 1.29 christos # define _Noreturn
380 1.29 christos # endif
381 1.29 christos #endif
382 1.29 christos
383 1.29 christos #if __STDC_VERSION__ < 199901 && !defined restrict
384 1.29 christos # define restrict /* empty */
385 1.27 christos #endif
386 1.27 christos
387 1.1 jtc /*
388 1.25 mlelstv ** Workarounds for compilers/systems.
389 1.1 jtc */
390 1.1 jtc
391 1.48 christos #ifndef EPOCH_LOCAL
392 1.48 christos # define EPOCH_LOCAL 0
393 1.48 christos #endif
394 1.48 christos #ifndef EPOCH_OFFSET
395 1.48 christos # define EPOCH_OFFSET 0
396 1.48 christos #endif
397 1.52 christos #ifndef RESERVE_STD_EXT_IDS
398 1.52 christos # define RESERVE_STD_EXT_IDS 0
399 1.52 christos #endif
400 1.52 christos
401 1.52 christos /* If standard C identifiers with external linkage (e.g., localtime)
402 1.52 christos are reserved and are not already being renamed anyway, rename them
403 1.52 christos as if compiling with '-Dtime_tz=time_t'. */
404 1.52 christos #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
405 1.52 christos # define time_tz time_t
406 1.52 christos #endif
407 1.48 christos
408 1.1 jtc /*
409 1.29 christos ** Compile with -Dtime_tz=T to build the tz package with a private
410 1.29 christos ** time_t type equivalent to T rather than the system-supplied time_t.
411 1.29 christos ** This debugging feature can test unusual design decisions
412 1.29 christos ** (e.g., time_t wider than 'long', or unsigned time_t) even on
413 1.29 christos ** typical platforms.
414 1.29 christos */
415 1.48 christos #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
416 1.51 christos # define TZ_TIME_T 1
417 1.51 christos #else
418 1.51 christos # define TZ_TIME_T 0
419 1.51 christos #endif
420 1.51 christos
421 1.51 christos #if TZ_TIME_T
422 1.36 christos # ifdef LOCALTIME_IMPLEMENTATION
423 1.29 christos static time_t sys_time(time_t *x) { return time(x); }
424 1.36 christos # endif
425 1.29 christos
426 1.39 christos typedef time_tz tz_time_t;
427 1.39 christos
428 1.29 christos # undef ctime
429 1.29 christos # define ctime tz_ctime
430 1.29 christos # undef ctime_r
431 1.29 christos # define ctime_r tz_ctime_r
432 1.29 christos # undef difftime
433 1.29 christos # define difftime tz_difftime
434 1.29 christos # undef gmtime
435 1.29 christos # define gmtime tz_gmtime
436 1.29 christos # undef gmtime_r
437 1.29 christos # define gmtime_r tz_gmtime_r
438 1.29 christos # undef localtime
439 1.29 christos # define localtime tz_localtime
440 1.29 christos # undef localtime_r
441 1.29 christos # define localtime_r tz_localtime_r
442 1.39 christos # undef localtime_rz
443 1.39 christos # define localtime_rz tz_localtime_rz
444 1.29 christos # undef mktime
445 1.29 christos # define mktime tz_mktime
446 1.39 christos # undef mktime_z
447 1.39 christos # define mktime_z tz_mktime_z
448 1.36 christos # undef offtime
449 1.36 christos # define offtime tz_offtime
450 1.36 christos # undef posix2time
451 1.36 christos # define posix2time tz_posix2time
452 1.39 christos # undef posix2time_z
453 1.39 christos # define posix2time_z tz_posix2time_z
454 1.51 christos # undef strftime
455 1.51 christos # define strftime tz_strftime
456 1.29 christos # undef time
457 1.29 christos # define time tz_time
458 1.36 christos # undef time2posix
459 1.36 christos # define time2posix tz_time2posix
460 1.39 christos # undef time2posix_z
461 1.39 christos # define time2posix_z tz_time2posix_z
462 1.29 christos # undef time_t
463 1.29 christos # define time_t tz_time_t
464 1.36 christos # undef timegm
465 1.36 christos # define timegm tz_timegm
466 1.36 christos # undef timelocal
467 1.36 christos # define timelocal tz_timelocal
468 1.36 christos # undef timeoff
469 1.36 christos # define timeoff tz_timeoff
470 1.39 christos # undef tzalloc
471 1.39 christos # define tzalloc tz_tzalloc
472 1.39 christos # undef tzfree
473 1.39 christos # define tzfree tz_tzfree
474 1.39 christos # undef tzset
475 1.39 christos # define tzset tz_tzset
476 1.39 christos # undef tzsetwall
477 1.39 christos # define tzsetwall tz_tzsetwall
478 1.51 christos # if HAVE_STRFTIME_L
479 1.51 christos # undef strftime_l
480 1.51 christos # define strftime_l tz_strftime_l
481 1.51 christos # endif
482 1.51 christos # if HAVE_TZNAME
483 1.51 christos # undef tzname
484 1.51 christos # define tzname tz_tzname
485 1.51 christos # endif
486 1.51 christos # if USG_COMPAT
487 1.51 christos # undef daylight
488 1.51 christos # define daylight tz_daylight
489 1.51 christos # undef timezone
490 1.51 christos # define timezone tz_timezone
491 1.51 christos # endif
492 1.51 christos # ifdef ALTZONE
493 1.51 christos # undef altzone
494 1.51 christos # define altzone tz_altzone
495 1.51 christos # endif
496 1.29 christos
497 1.29 christos char *ctime(time_t const *);
498 1.29 christos char *ctime_r(time_t const *, char *);
499 1.51 christos double difftime(time_t, time_t) ATTRIBUTE_CONST;
500 1.51 christos size_t strftime(char *restrict, size_t, char const *restrict,
501 1.51 christos struct tm const *restrict);
502 1.51 christos # if HAVE_STRFTIME_L
503 1.51 christos size_t strftime_l(char *restrict, size_t, char const *restrict,
504 1.51 christos struct tm const *restrict, locale_t);
505 1.51 christos # endif
506 1.29 christos struct tm *gmtime(time_t const *);
507 1.29 christos struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
508 1.29 christos struct tm *localtime(time_t const *);
509 1.29 christos struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
510 1.29 christos time_t mktime(struct tm *);
511 1.36 christos time_t time(time_t *);
512 1.39 christos void tzset(void);
513 1.36 christos #endif
514 1.29 christos
515 1.45 christos #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
516 1.45 christos extern char *asctime_r(struct tm const *restrict, char *restrict);
517 1.36 christos #endif
518 1.36 christos
519 1.51 christos #ifndef HAVE_DECL_ENVIRON
520 1.51 christos # if defined environ || defined __USE_GNU
521 1.51 christos # define HAVE_DECL_ENVIRON 1
522 1.51 christos # else
523 1.51 christos # define HAVE_DECL_ENVIRON 0
524 1.51 christos # endif
525 1.51 christos #endif
526 1.51 christos
527 1.51 christos #if !HAVE_DECL_ENVIRON
528 1.51 christos extern char **environ;
529 1.51 christos #endif
530 1.51 christos
531 1.51 christos #if TZ_TIME_T || !HAVE_POSIX_DECLS
532 1.51 christos # if HAVE_TZNAME
533 1.51 christos extern char *tzname[];
534 1.51 christos # endif
535 1.51 christos # if USG_COMPAT
536 1.44 christos extern long timezone;
537 1.44 christos extern int daylight;
538 1.44 christos # endif
539 1.44 christos #endif
540 1.45 christos
541 1.51 christos #ifdef ALTZONE
542 1.44 christos extern long altzone;
543 1.44 christos #endif
544 1.44 christos
545 1.36 christos /*
546 1.36 christos ** The STD_INSPIRED functions are similar, but most also need
547 1.36 christos ** declarations if time_tz is defined.
548 1.36 christos */
549 1.36 christos
550 1.36 christos #ifdef STD_INSPIRED
551 1.51 christos # if TZ_TIME_T || !defined tzsetwall
552 1.36 christos void tzsetwall(void);
553 1.36 christos # endif
554 1.51 christos # if TZ_TIME_T || !defined offtime
555 1.36 christos struct tm *offtime(time_t const *, long);
556 1.36 christos # endif
557 1.51 christos # if TZ_TIME_T || !defined timegm
558 1.36 christos time_t timegm(struct tm *);
559 1.36 christos # endif
560 1.51 christos # if TZ_TIME_T || !defined timelocal
561 1.36 christos time_t timelocal(struct tm *);
562 1.36 christos # endif
563 1.51 christos # if TZ_TIME_T || !defined timeoff
564 1.36 christos time_t timeoff(struct tm *, long);
565 1.36 christos # endif
566 1.51 christos # if TZ_TIME_T || !defined time2posix
567 1.36 christos time_t time2posix(time_t);
568 1.36 christos # endif
569 1.51 christos # if TZ_TIME_T || !defined posix2time
570 1.36 christos time_t posix2time(time_t);
571 1.36 christos # endif
572 1.36 christos #endif
573 1.36 christos
574 1.36 christos /* Infer TM_ZONE on systems where this information is known, but suppress
575 1.36 christos guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
576 1.36 christos #if (defined __GLIBC__ \
577 1.36 christos || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
578 1.36 christos || (defined __APPLE__ && defined __MACH__))
579 1.36 christos # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
580 1.36 christos # define TM_GMTOFF tm_gmtoff
581 1.36 christos # endif
582 1.36 christos # if !defined TM_ZONE && !defined NO_TM_ZONE
583 1.36 christos # define TM_ZONE tm_zone
584 1.36 christos # endif
585 1.36 christos #endif
586 1.36 christos
587 1.36 christos /*
588 1.36 christos ** Define functions that are ABI compatible with NetBSD but have
589 1.36 christos ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
590 1.36 christos ** and labors under the misconception that 'const timezone_t' is a
591 1.36 christos ** pointer to a constant. This use of 'const' is ineffective, so it
592 1.36 christos ** is not done here. What we call 'struct state' NetBSD calls
593 1.36 christos ** 'struct __state', but this is a private name so it doesn't matter.
594 1.36 christos */
595 1.37 christos #ifndef __NetBSD__
596 1.36 christos #if NETBSD_INSPIRED
597 1.36 christos typedef struct state *timezone_t;
598 1.36 christos struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
599 1.36 christos struct tm *restrict);
600 1.36 christos time_t mktime_z(timezone_t restrict, struct tm *restrict);
601 1.36 christos timezone_t tzalloc(char const *);
602 1.36 christos void tzfree(timezone_t);
603 1.36 christos # ifdef STD_INSPIRED
604 1.51 christos # if TZ_TIME_T || !defined posix2time_z
605 1.36 christos time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
606 1.36 christos # endif
607 1.51 christos # if TZ_TIME_T || !defined time2posix_z
608 1.36 christos time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
609 1.36 christos # endif
610 1.36 christos # endif
611 1.29 christos #endif
612 1.37 christos #endif
613 1.29 christos
614 1.29 christos /*
615 1.1 jtc ** Finally, some convenience items.
616 1.1 jtc */
617 1.1 jtc
618 1.51 christos #if HAVE_STDBOOL_H
619 1.51 christos # include <stdbool.h>
620 1.51 christos #else
621 1.36 christos # define true 1
622 1.36 christos # define false 0
623 1.36 christos # define bool int
624 1.36 christos #endif
625 1.1 jtc
626 1.5 jtc #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
627 1.28 christos #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
628 1.41 christos #define TWOS_COMPLEMENT(t) (/*CONSTCOND*/(t) ~ (t) 0 < 0)
629 1.41 christos
630 1.41 christos /* Max and min values of the integer type T, of which only the bottom
631 1.41 christos B bits are used, and where the highest-order used bit is considered
632 1.41 christos to be a sign bit if T is signed. */
633 1.41 christos #define MAXVAL(t, b) /*LINTED*/ \
634 1.41 christos ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
635 1.41 christos - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
636 1.41 christos #define MINVAL(t, b) \
637 1.41 christos ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
638 1.41 christos
639 1.51 christos /* The extreme time values, assuming no padding. */
640 1.51 christos #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
641 1.51 christos #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
642 1.51 christos
643 1.51 christos /* The extreme time values. These are macros, not constants, so that
644 1.51 christos any portability problem occur only when compiling .c files that use
645 1.51 christos the macros, which is safer for applications that need only zdump and zic.
646 1.51 christos This implementation assumes no padding if time_t is signed and
647 1.51 christos either the compiler lacks support for _Generic or time_t is not one
648 1.51 christos of the standard signed integer types. */
649 1.51 christos #if HAVE_GENERIC
650 1.51 christos # define TIME_T_MIN \
651 1.51 christos _Generic((time_t) 0, \
652 1.51 christos signed char: SCHAR_MIN, short: SHRT_MIN, \
653 1.51 christos int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
654 1.51 christos default: TIME_T_MIN_NO_PADDING)
655 1.51 christos # define TIME_T_MAX \
656 1.51 christos (TYPE_SIGNED(time_t) \
657 1.51 christos ? _Generic((time_t) 0, \
658 1.51 christos signed char: SCHAR_MAX, short: SHRT_MAX, \
659 1.51 christos int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
660 1.51 christos default: TIME_T_MAX_NO_PADDING) \
661 1.51 christos : (time_t) -1)
662 1.48 christos #else
663 1.51 christos # define TIME_T_MIN TIME_T_MIN_NO_PADDING
664 1.51 christos # define TIME_T_MAX TIME_T_MAX_NO_PADDING
665 1.48 christos #endif
666 1.36 christos
667 1.1 jtc /*
668 1.1 jtc ** 302 / 1000 is log10(2.0) rounded up.
669 1.5 jtc ** Subtract one for the sign bit if the type is signed;
670 1.5 jtc ** add one for integer division truncation;
671 1.5 jtc ** add one more for a minus sign if the type is signed.
672 1.1 jtc */
673 1.1 jtc #define INT_STRLEN_MAXIMUM(type) \
674 1.25 mlelstv ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
675 1.25 mlelstv 1 + TYPE_SIGNED(type))
676 1.1 jtc
677 1.1 jtc /*
678 1.1 jtc ** INITIALIZE(x)
679 1.1 jtc */
680 1.1 jtc
681 1.35 christos #if defined(__GNUC__) || defined(__lint__)
682 1.34 christos # define INITIALIZE(x) ((x) = 0)
683 1.34 christos #else
684 1.34 christos # define INITIALIZE(x)
685 1.34 christos #endif
686 1.6 jtc
687 1.36 christos #ifndef UNINIT_TRAP
688 1.36 christos # define UNINIT_TRAP 0
689 1.36 christos #endif
690 1.36 christos
691 1.6 jtc /*
692 1.6 jtc ** For the benefit of GNU folk...
693 1.34 christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
694 1.6 jtc ** The default is to use gettext if available, and use MSGID otherwise.
695 1.6 jtc */
696 1.6 jtc
697 1.25 mlelstv #if HAVE_GETTEXT
698 1.6 jtc #define _(msgid) gettext(msgid)
699 1.25 mlelstv #else /* !HAVE_GETTEXT */
700 1.6 jtc #define _(msgid) msgid
701 1.25 mlelstv #endif /* !HAVE_GETTEXT */
702 1.6 jtc
703 1.34 christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
704 1.34 christos # define TZ_DOMAIN "tz"
705 1.34 christos #endif
706 1.17 kleink
707 1.17 kleink #if HAVE_INCOMPATIBLE_CTIME_R
708 1.17 kleink #undef asctime_r
709 1.17 kleink #undef ctime_r
710 1.25 mlelstv char *asctime_r(struct tm const *, char *);
711 1.25 mlelstv char *ctime_r(time_t const *, char *);
712 1.17 kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
713 1.1 jtc
714 1.50 christos /* Handy macros that are independent of tzfile implementation. */
715 1.50 christos
716 1.25 mlelstv #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
717 1.50 christos
718 1.50 christos #define SECSPERMIN 60
719 1.50 christos #define MINSPERHOUR 60
720 1.50 christos #define HOURSPERDAY 24
721 1.50 christos #define DAYSPERWEEK 7
722 1.50 christos #define DAYSPERNYEAR 365
723 1.50 christos #define DAYSPERLYEAR 366
724 1.50 christos #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
725 1.50 christos #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
726 1.50 christos #define MONSPERYEAR 12
727 1.50 christos
728 1.50 christos #define TM_SUNDAY 0
729 1.50 christos #define TM_MONDAY 1
730 1.50 christos #define TM_TUESDAY 2
731 1.50 christos #define TM_WEDNESDAY 3
732 1.50 christos #define TM_THURSDAY 4
733 1.50 christos #define TM_FRIDAY 5
734 1.50 christos #define TM_SATURDAY 6
735 1.50 christos
736 1.50 christos #define TM_JANUARY 0
737 1.50 christos #define TM_FEBRUARY 1
738 1.50 christos #define TM_MARCH 2
739 1.50 christos #define TM_APRIL 3
740 1.50 christos #define TM_MAY 4
741 1.50 christos #define TM_JUNE 5
742 1.50 christos #define TM_JULY 6
743 1.50 christos #define TM_AUGUST 7
744 1.50 christos #define TM_SEPTEMBER 8
745 1.50 christos #define TM_OCTOBER 9
746 1.50 christos #define TM_NOVEMBER 10
747 1.50 christos #define TM_DECEMBER 11
748 1.50 christos
749 1.50 christos #define TM_YEAR_BASE 1900
750 1.50 christos
751 1.50 christos #define EPOCH_YEAR 1970
752 1.50 christos #define EPOCH_WDAY TM_THURSDAY
753 1.50 christos
754 1.50 christos #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
755 1.50 christos
756 1.50 christos /*
757 1.50 christos ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
758 1.50 christos ** isleap(y) == isleap(y % 400)
759 1.50 christos ** and so
760 1.50 christos ** isleap(a + b) == isleap((a + b) % 400)
761 1.50 christos ** or
762 1.50 christos ** isleap(a + b) == isleap(a % 400 + b % 400)
763 1.50 christos ** This is true even if % means modulo rather than Fortran remainder
764 1.51 christos ** (which is allowed by C89 but not by C99 or later).
765 1.50 christos ** We use this to avoid addition overflow problems.
766 1.50 christos */
767 1.50 christos
768 1.50 christos #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
769 1.50 christos
770 1.25 mlelstv
771 1.25 mlelstv /*
772 1.25 mlelstv ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
773 1.25 mlelstv */
774 1.25 mlelstv
775 1.25 mlelstv #define AVGSECSPERYEAR 31556952L
776 1.50 christos #define SECSPERREPEAT \
777 1.50 christos ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
778 1.25 mlelstv #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
779 1.25 mlelstv
780 1.1 jtc #endif /* !defined PRIVATE_H */
781