PostgreSQL Source Code git master
Loading...
Searching...
No Matches
private.h
Go to the documentation of this file.
1/* Private header for tzdb code. */
2
3#ifndef PRIVATE_H
4
5#define PRIVATE_H
6
7/*
8 * This file is in the public domain, so clarified as of
9 * 1996-06-05 by Arthur David Olson.
10 *
11 * IDENTIFICATION
12 * src/timezone/private.h
13 */
14
15/*
16 * This header is for use ONLY with the time conversion code.
17 * There is no guarantee that it will remain unchanged,
18 * or that it will remain at all.
19 * Do NOT copy it to any system include directory.
20 * Thank you!
21 */
22
23/*
24 * IANA now expects this symbol to be defined via a compiler switch,
25 * but in PG we don't want to do it that way.
26 */
27#define TZDEFAULT "/etc/localtime"
28
29/*
30 * IANA messes with some feature-test macros here, but in PG we want pretty
31 * much all of that to be done by PG's configure script and c.h header.
32 */
33
34/*
35 * For pre-C23 compilers, a substitute for static_assert.
36 * Some of these compilers may warn if it is used outside the top level.
37 */
38#if __STDC_VERSION__ < 202311 && !defined static_assert
39#define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
40#endif
41
42/* This string was in the Factory zone through version 2016f. */
43#ifndef GRANDPARENTED
44#define GRANDPARENTED "Local time zone must be set--see zic manual page"
45#endif
46
47/*
48 * IANA has a bunch of HAVE_FOO #defines here, but in PG we want pretty
49 * much all of that to be done by PG's configure script and c.h header.
50 */
51
52#define ATTRIBUTE_FALLTHROUGH pg_fallthrough
53#define ATTRIBUTE_MAYBE_UNUSED pg_attribute_unused()
54#define ATTRIBUTE_NORETURN pg_noreturn
55#define ATTRIBUTE_PURE_114833
56#define ATTRIBUTE_PURE_114833_HACK
57
58/*
59 * Nested includes
60 * In PG, much of this was already done by c.h.
61 */
62
63#include "pgtime.h"
64
65#include <limits.h> /* for CHAR_BIT et al. */
66#include <unistd.h> /* for F_OK and R_OK */
67
68#ifndef EINVAL
69#define EINVAL ERANGE
70#endif
71
72#ifndef ELOOP
73#define ELOOP EINVAL
74#endif
75#ifndef ENAMETOOLONG
76#define ENAMETOOLONG EINVAL
77#endif
78#ifndef ENOMEM
79#define ENOMEM EINVAL
80#endif
81#ifndef ENOTCAPABLE
82#define ENOTCAPABLE EINVAL
83#endif
84#ifndef ENOTSUP
85#define ENOTSUP EINVAL
86#endif
87#ifndef EOVERFLOW
88#define EOVERFLOW EINVAL
89#endif
90
91/*
92 * The maximum size of any created object, as a signed integer.
93 * Although the C standard does not outright prohibit larger objects,
94 * behavior is undefined if the result of pointer subtraction does not
95 * fit into ptrdiff_t, and the code assumes in several places that
96 * pointer subtraction works. As a practical matter it's OK to not
97 * support objects larger than this.
98 */
99#define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
100
101/*
102 * Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
103 * hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG.
104 */
105#if !defined HAVE_STDCKDINT_H && defined __has_include
106#if __has_include(<stdckdint.h>)
107#define HAVE_STDCKDINT_H 1
108#endif
109#endif
110#ifdef HAVE_STDCKDINT_H
111#if HAVE_STDCKDINT_H
112#include <stdckdint.h>
113#endif
114#elif defined __EDG__
115/* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
116#elif defined __has_builtin
117#if __has_builtin(__builtin_add_overflow)
118#define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
119#endif
120#if __has_builtin(__builtin_sub_overflow)
121#define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
122#endif
123#if __has_builtin(__builtin_mul_overflow)
124#define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
125#endif
126#elif 7 <= __GNUC__
127#define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
128#define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
129#define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
130#endif
131
132
133/*
134 * In PG, we always have these fields in struct pg_tm.
135 */
136#define TM_GMTOFF tm_gmtoff
137#define TM_ZONE tm_zone
138
139
140/*
141 * Finally, some convenience items.
142 */
143
144#define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
145#define TYPE_SIGNED(type) (((type) -1) < 0)
146#define TWOS_COMPLEMENT(type) (TYPE_SIGNED (type) && (! ~ (type) -1))
147
148/*
149 * Minimum and maximum of two values. Use lower case to avoid
150 * naming clashes with standard include files.
151 */
152#undef max
153#undef min
154#define max(a, b) ((a) > (b) ? (a) : (b))
155#define min(a, b) ((a) < (b) ? (a) : (b))
156
157/*
158 * Max and min values of the integer type T, of which only the bottom
159 * B bits are used, and where the highest-order used bit is considered
160 * to be a sign bit if T is signed.
161 */
162#define MAXVAL(t, b) \
163 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
164 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
165#define MINVAL(t, b) \
166 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
167
168/* The extreme time values, assuming no padding. */
169#define TIME_T_MIN MINVAL(pg_time_t, TYPE_BIT(pg_time_t))
170#define TIME_T_MAX MAXVAL(pg_time_t, TYPE_BIT(pg_time_t))
171
172/*
173 * 302 / 1000 is log10(2.0) rounded up.
174 * Subtract one for the sign bit if the type is signed;
175 * add one for integer division truncation;
176 * add one more for a minus sign if the type is signed.
177 */
178#define INT_STRLEN_MAXIMUM(type) \
179 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
180 1 + TYPE_SIGNED(type))
181
182/*
183 * INITIALIZE(x)
184 */
185#define INITIALIZE(x) ((x) = 0)
186
187/* Some platforms provide unreachable(), but let's rely on our own version */
188#undef unreachable
189#define unreachable() pg_unreachable()
190
191/*
192 * For the benefit of GNU folk...
193 * '_(MSGID)' uses the current locale's message library string for MSGID.
194 * The default is to use gettext if available, and use MSGID otherwise.
195 * For PG's purposes, there is no need to support localization in zic.
196 */
197
198#undef _
199#define _(msgid) (msgid)
200#define N_(msgid) (msgid)
201
202/* Handy constants that are independent of tzfile implementation. */
203
204/* 2**31 - 1 as a signed integer, and usable in #if. */
205#define TWO_31_MINUS_1 2147483647
206
207enum
208{
217 YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
219
220#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
221
222#define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
223#define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
224#define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
225
226/*
227 * How many years to generate (in zic.c) or search through (in localtime.c).
228 * This is two years larger than the obvious 400, to avoid edge cases.
229 * E.g., suppose a rule applies from 2012 on with transitions
230 * in March and September, plus one-off transitions in November 2013,
231 * and suppose the rule cannot be expressed as a proleptic TZ string.
232 * If zic looked only at the last 400 years, it would set max_year=2413,
233 * with the intent that the 400 years 2014 through 2413 will be repeated.
234 * The last transition listed in the tzfile would be in 2413-09,
235 * less than 400 years after the last one-off transition in 2013-11.
236 * Two years is not overkill for localtime.c, as a one-year bump
237 * would mishandle 2023d's America/Ciudad_Juarez for November 2422.
238 */
239enum
240{
242
243enum
244{
253
254enum
255{
269
270enum
271{
277
278#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
279
280/*
281 * Since everything in isleap is modulo 400 (or a factor of 400), we know that
282 * isleap(y) == isleap(y % 400)
283 * and so
284 * isleap(a + b) == isleap((a + b) % 400)
285 * or
286 * isleap(a + b) == isleap(a % 400 + b % 400)
287 * This is true even if % means modulo rather than Fortran remainder
288 * (which is allowed by C89 but not by C99 or later).
289 * We use this to avoid addition overflow problems.
290 */
291
292#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
293
294#endif /* !defined PRIVATE_H */
@ TM_WDAY_BASE
Definition private.h:273
@ EPOCH_WDAY
Definition private.h:275
@ EPOCH_YEAR
Definition private.h:274
@ TM_YEAR_BASE
Definition private.h:272
@ DAYSPERNYEAR
Definition private.h:214
@ DAYSPERLYEAR
Definition private.h:215
@ DAYSPERWEEK
Definition private.h:213
@ SECSPERMIN
Definition private.h:209
@ MONSPERYEAR
Definition private.h:216
@ YEARSPERREPEAT
Definition private.h:217
@ SECSPERHOUR
Definition private.h:211
@ MINSPERHOUR
Definition private.h:210
@ HOURSPERDAY
Definition private.h:212
@ years_of_observations
Definition private.h:241
@ TM_JANUARY
Definition private.h:256
@ TM_MAY
Definition private.h:260
@ TM_DECEMBER
Definition private.h:267
@ TM_MARCH
Definition private.h:258
@ TM_APRIL
Definition private.h:259
@ TM_OCTOBER
Definition private.h:265
@ TM_NOVEMBER
Definition private.h:266
@ TM_SEPTEMBER
Definition private.h:264
@ TM_JULY
Definition private.h:262
@ TM_FEBRUARY
Definition private.h:257
@ TM_JUNE
Definition private.h:261
@ TM_AUGUST
Definition private.h:263
@ TM_SUNDAY
Definition private.h:245
@ TM_TUESDAY
Definition private.h:247
@ TM_SATURDAY
Definition private.h:251
@ TM_FRIDAY
Definition private.h:250
@ TM_THURSDAY
Definition private.h:249
@ TM_MONDAY
Definition private.h:246
@ TM_WEDNESDAY
Definition private.h:248