🌐 AI搜索 & 代理 主页
Skip to content

Commit e6be843

Browse files
committed
Update timezone to C99
This reverts changes done in PostgreSQL over the upstream code to avoid relying on C99 <stdint.h> and <inttypes.h>. In passing, there were a few other minor and cosmetic changes that I left in to improve alignment with upstream, including some C11 feature use (_Noreturn). Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/9ad2749f-77ab-4ecb-a321-1ca915480b05%40eisentraut.org
1 parent 97e04c7 commit e6be843

File tree

6 files changed

+136
-161
lines changed

6 files changed

+136
-161
lines changed

src/timezone/README

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ fixed that.)
7979
includes relying on configure's results rather than hand-hacked
8080
#defines (see private.h in particular).
8181

82-
* Similarly, avoid relying on <stdint.h> features that may not exist on old
83-
systems. In particular this means using Postgres' definitions of the int32
84-
and int64 typedefs, not int_fast32_t/int_fast64_t. Likewise we use
85-
PG_INT32_MIN/MAX not INT32_MIN/MAX. (Once we desupport all PG versions
86-
that don't require C99, it'd be practical to rely on <stdint.h> and remove
87-
this set of diffs; but that day is not yet.)
88-
8982
* Since Postgres is typically built on a system that has its own copy
9083
of the <time.h> functions, we must avoid conflicting with those. This
9184
mandates renaming typedef time_t to pg_time_t, and similarly for most
@@ -119,13 +112,6 @@ to first run the tzcode source files through a sed filter like this:
119112
-e 's|^\*/| */|' \
120113
-e 's/\bregister[ \t]//g' \
121114
-e 's/\bATTRIBUTE_PURE[ \t]//g' \
122-
-e 's/int_fast32_t/int32/g' \
123-
-e 's/int_fast64_t/int64/g' \
124-
-e 's/intmax_t/int64/g' \
125-
-e 's/INT32_MIN/PG_INT32_MIN/g' \
126-
-e 's/INT32_MAX/PG_INT32_MAX/g' \
127-
-e 's/INTMAX_MIN/PG_INT64_MIN/g' \
128-
-e 's/INTMAX_MAX/PG_INT64_MAX/g' \
129115
-e 's/struct[ \t]+tm\b/struct pg_tm/g' \
130116
-e 's/\btime_t\b/pg_time_t/g' \
131117
-e 's/lineno/lineno_t/g' \

src/timezone/localtime.c

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum r_type
6666
{
6767
JULIAN_DAY, /* Jn = Julian day */
6868
DAY_OF_YEAR, /* n = day of year */
69-
MONTH_NTH_DAY_OF_WEEK, /* Mm.n.d = month, week, day of week */
69+
MONTH_NTH_DAY_OF_WEEK /* Mm.n.d = month, week, day of week */
7070
};
7171

7272
struct rule
@@ -75,20 +75,20 @@ struct rule
7575
int r_day; /* day number of rule */
7676
int r_week; /* week number of rule */
7777
int r_mon; /* month number of rule */
78-
int32 r_time; /* transition time of rule */
78+
int_fast32_t r_time; /* transition time of rule */
7979
};
8080

8181
/*
8282
* Prototypes for static functions.
8383
*/
8484

85-
static struct pg_tm *gmtsub(pg_time_t const *timep, int32 offset,
85+
static struct pg_tm *gmtsub(pg_time_t const *timep, int_fast32_t offset,
8686
struct pg_tm *tmp);
8787
static bool increment_overflow(int *ip, int j);
88-
static bool increment_overflow_time(pg_time_t *tp, int32 j);
89-
static int64 leapcorr(struct state const *sp, pg_time_t t);
88+
static bool increment_overflow_time(pg_time_t *tp, int_fast32_t j);
89+
static int_fast64_t leapcorr(struct state const *sp, pg_time_t);
9090
static struct pg_tm *timesub(pg_time_t const *timep,
91-
int32 offset, struct state const *sp,
91+
int_fast32_t offset, struct state const *sp,
9292
struct pg_tm *tmp);
9393
static bool typesequiv(struct state const *sp, int a, int b);
9494

@@ -105,7 +105,7 @@ static struct pg_tm tm;
105105

106106
/* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX. */
107107
static void
108-
init_ttinfo(struct ttinfo *s, int32 utoff, bool isdst, int desigidx)
108+
init_ttinfo(struct ttinfo *s, int_fast32_t utoff, bool isdst, int desigidx)
109109
{
110110
s->tt_utoff = utoff;
111111
s->tt_isdst = isdst;
@@ -114,15 +114,15 @@ init_ttinfo(struct ttinfo *s, int32 utoff, bool isdst, int desigidx)
114114
s->tt_ttisut = false;
115115
}
116116

117-
static int32
117+
static int_fast32_t
118118
detzcode(const char *const codep)
119119
{
120-
int32 result;
120+
int_fast32_t result;
121121
int i;
122-
int32 one = 1;
123-
int32 halfmaxval = one << (32 - 2);
124-
int32 maxval = halfmaxval - 1 + halfmaxval;
125-
int32 minval = -1 - maxval;
122+
int_fast32_t one = 1;
123+
int_fast32_t halfmaxval = one << (32 - 2);
124+
int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
125+
int_fast32_t minval = -1 - maxval;
126126

127127
result = codep[0] & 0x7f;
128128
for (i = 1; i < 4; ++i)
@@ -134,21 +134,21 @@ detzcode(const char *const codep)
134134
* Do two's-complement negation even on non-two's-complement machines.
135135
* If the result would be minval - 1, return minval.
136136
*/
137-
result -= !TWOS_COMPLEMENT(int32) && result != 0;
137+
result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
138138
result += minval;
139139
}
140140
return result;
141141
}
142142

143-
static int64
143+
static int_fast64_t
144144
detzcode64(const char *const codep)
145145
{
146-
uint64 result;
146+
uint_fast64_t result;
147147
int i;
148-
int64 one = 1;
149-
int64 halfmaxval = one << (64 - 2);
150-
int64 maxval = halfmaxval - 1 + halfmaxval;
151-
int64 minval = -TWOS_COMPLEMENT(int64) - maxval;
148+
int_fast64_t one = 1;
149+
int_fast64_t halfmaxval = one << (64 - 2);
150+
int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
151+
int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
152152

153153
result = codep[0] & 0x7f;
154154
for (i = 1; i < 8; ++i)
@@ -160,7 +160,7 @@ detzcode64(const char *const codep)
160160
* Do two's-complement negation even on non-two's-complement machines.
161161
* If the result would be minval - 1, return minval.
162162
*/
163-
result -= !TWOS_COMPLEMENT(int64) && result != 0;
163+
result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
164164
result += minval;
165165
}
166166
return result;
@@ -246,14 +246,14 @@ tzloadbody(char const *name, char *canonname, struct state *sp, bool doextend,
246246
return errno;
247247
for (stored = 4; stored <= 8; stored *= 2)
248248
{
249-
int32 ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
250-
int32 ttisutcnt = detzcode(up->tzhead.tzh_ttisutcnt);
251-
int64 prevtr = 0;
252-
int32 prevcorr = 0;
253-
int32 leapcnt = detzcode(up->tzhead.tzh_leapcnt);
254-
int32 timecnt = detzcode(up->tzhead.tzh_timecnt);
255-
int32 typecnt = detzcode(up->tzhead.tzh_typecnt);
256-
int32 charcnt = detzcode(up->tzhead.tzh_charcnt);
249+
int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
250+
int_fast32_t ttisutcnt = detzcode(up->tzhead.tzh_ttisutcnt);
251+
int_fast64_t prevtr = 0;
252+
int_fast32_t prevcorr = 0;
253+
int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
254+
int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
255+
int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
256+
int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
257257
char const *p = up->buf + tzheadsize;
258258

259259
/*
@@ -291,7 +291,7 @@ tzloadbody(char const *name, char *canonname, struct state *sp, bool doextend,
291291
timecnt = 0;
292292
for (i = 0; i < sp->timecnt; ++i)
293293
{
294-
int64 at
294+
int_fast64_t at
295295
= stored == 4 ? detzcode(p) : detzcode64(p);
296296

297297
sp->types[i] = at <= TIME_T_MAX;
@@ -350,8 +350,8 @@ tzloadbody(char const *name, char *canonname, struct state *sp, bool doextend,
350350
leapcnt = 0;
351351
for (i = 0; i < sp->leapcnt; ++i)
352352
{
353-
int64 tr = stored == 4 ? detzcode(p) : detzcode64(p);
354-
int32 corr = detzcode(p + stored);
353+
int_fast64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
354+
int_fast32_t corr = detzcode(p + stored);
355355

356356
p += stored + 4;
357357
/* Leap seconds cannot occur before the Epoch. */
@@ -421,6 +421,7 @@ tzloadbody(char const *name, char *canonname, struct state *sp, bool doextend,
421421
up->buf[nread - 1] = '\0';
422422
if (tzparse(&up->buf[1], ts, false))
423423
{
424+
424425
/*
425426
* Attempt to reuse existing abbreviations. Without this,
426427
* America/Anchorage would be right on the edge after 2037 when
@@ -583,7 +584,7 @@ tzloadbody(char const *name, char *canonname, struct state *sp, bool doextend,
583584
* given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
584585
*/
585586
int
586-
tzload(const char *name, char *canonname, struct state *sp, bool doextend)
587+
tzload(char const *name, char *canonname, struct state *sp, bool doextend)
587588
{
588589
union local_storage *lsp = malloc(sizeof *lsp);
589590

@@ -707,7 +708,7 @@ getnum(const char *strp, int *const nump, const int min, const int max)
707708
*/
708709

709710
static const char *
710-
getsecs(const char *strp, int32 *const secsp)
711+
getsecs(const char *strp, int_fast32_t *const secsp)
711712
{
712713
int num;
713714

@@ -719,7 +720,7 @@ getsecs(const char *strp, int32 *const secsp)
719720
strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
720721
if (strp == NULL)
721722
return NULL;
722-
*secsp = num * (int32) SECSPERHOUR;
723+
*secsp = num * (int_fast32_t) SECSPERHOUR;
723724
if (*strp == ':')
724725
{
725726
++strp;
@@ -748,7 +749,7 @@ getsecs(const char *strp, int32 *const secsp)
748749
*/
749750

750751
static const char *
751-
getoffset(const char *strp, int32 *const offsetp)
752+
getoffset(const char *strp, int_fast32_t *const offsetp)
752753
{
753754
bool neg = false;
754755

@@ -835,12 +836,12 @@ getrule(const char *strp, struct rule *const rulep)
835836
* effect, calculate the year-relative time that rule takes effect.
836837
*/
837838

838-
static int32
839+
static int_fast32_t
839840
transtime(const int year, const struct rule *const rulep,
840-
const int32 offset)
841+
const int_fast32_t offset)
841842
{
842843
bool leapyear;
843-
int32 value;
844+
int_fast32_t value;
844845
int i;
845846
int d,
846847
m1,
@@ -940,8 +941,8 @@ tzparse(const char *name, struct state *sp, bool lastditch)
940941
size_t stdlen;
941942
size_t dstlen;
942943
size_t charcnt;
943-
int32 stdoffset;
944-
int32 dstoffset;
944+
int_fast32_t stdoffset;
945+
int_fast32_t dstoffset;
945946
char *cp;
946947
bool load_ok;
947948

@@ -1033,7 +1034,7 @@ tzparse(const char *name, struct state *sp, bool lastditch)
10331034
int yearlim;
10341035
int timecnt;
10351036
pg_time_t janfirst;
1036-
int32 janoffset = 0;
1037+
int_fast32_t janoffset = 0;
10371038
int yearbeg;
10381039

10391040
++name;
@@ -1059,7 +1060,7 @@ tzparse(const char *name, struct state *sp, bool lastditch)
10591060

10601061
do
10611062
{
1062-
int32 yearsecs
1063+
int_fast32_t yearsecs
10631064
= year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
10641065

10651066
yearbeg--;
@@ -1073,17 +1074,17 @@ tzparse(const char *name, struct state *sp, bool lastditch)
10731074
yearlim = yearbeg + YEARSPERREPEAT + 1;
10741075
for (year = yearbeg; year < yearlim; year++)
10751076
{
1076-
int32
1077+
int_fast32_t
10771078
starttime = transtime(year, &start, stdoffset),
10781079
endtime = transtime(year, &end, dstoffset);
1079-
int32
1080+
int_fast32_t
10801081
yearsecs = (year_lengths[isleap(year)]
10811082
* SECSPERDAY);
10821083
bool reversed = endtime < starttime;
10831084

10841085
if (reversed)
10851086
{
1086-
int32 swap = starttime;
1087+
int_fast32_t swap = starttime;
10871088

10881089
starttime = endtime;
10891090
endtime = swap;
@@ -1126,9 +1127,9 @@ tzparse(const char *name, struct state *sp, bool lastditch)
11261127
}
11271128
else
11281129
{
1129-
int32 theirstdoffset;
1130-
int32 theirdstoffset;
1131-
int32 theiroffset;
1130+
int_fast32_t theirstdoffset;
1131+
int_fast32_t theirdstoffset;
1132+
int_fast32_t theiroffset;
11321133
bool isdst;
11331134
int i;
11341135
int j;
@@ -1290,7 +1291,7 @@ localsub(struct state const *sp, pg_time_t const *timep,
12901291
result = localsub(sp, &newt, tmp);
12911292
if (result)
12921293
{
1293-
int64 newy;
1294+
int_fast64_t newy;
12941295

12951296
newy = result->tm_year;
12961297
if (t < sp->ats[0])
@@ -1354,7 +1355,7 @@ pg_localtime(const pg_time_t *timep, const pg_tz *tz)
13541355
*/
13551356

13561357
static struct pg_tm *
1357-
gmtsub(pg_time_t const *timep, int32 offset,
1358+
gmtsub(pg_time_t const *timep, int_fast32_t offset,
13581359
struct pg_tm *tmp)
13591360
{
13601361
struct pg_tm *result;
@@ -1411,16 +1412,16 @@ leaps_thru_end_of(const int y)
14111412
}
14121413

14131414
static struct pg_tm *
1414-
timesub(const pg_time_t *timep, int32 offset,
1415+
timesub(const pg_time_t *timep, int_fast32_t offset,
14151416
const struct state *sp, struct pg_tm *tmp)
14161417
{
14171418
const struct lsinfo *lp;
14181419
pg_time_t tdays;
14191420
int idays; /* unsigned would be so 2003 */
1420-
int64 rem;
1421+
int_fast64_t rem;
14211422
int y;
14221423
const int *ip;
1423-
int64 corr;
1424+
int_fast64_t corr;
14241425
bool hit;
14251426
int i;
14261427

@@ -1554,7 +1555,7 @@ increment_overflow(int *ip, int j)
15541555
}
15551556

15561557
static bool
1557-
increment_overflow_time(pg_time_t *tp, int32 j)
1558+
increment_overflow_time(pg_time_t *tp, int_fast32_t j)
15581559
{
15591560
/*----------
15601561
* This is like
@@ -1570,7 +1571,7 @@ increment_overflow_time(pg_time_t *tp, int32 j)
15701571
return false;
15711572
}
15721573

1573-
static int64
1574+
static int_fast64_t
15741575
leapcorr(struct state const *sp, pg_time_t t)
15751576
{
15761577
struct lsinfo const *lp;

src/timezone/pgtz.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
struct ttinfo
2727
{ /* time type information */
28-
int32 tt_utoff; /* UT offset in seconds */
28+
int_fast32_t tt_utoff; /* UT offset in seconds */
2929
bool tt_isdst; /* used to set tm_isdst */
3030
int tt_desigidx; /* abbreviation list index */
3131
bool tt_ttisstd; /* transition is std time */
@@ -35,7 +35,7 @@ struct ttinfo
3535
struct lsinfo
3636
{ /* leap second information */
3737
pg_time_t ls_trans; /* transition time */
38-
int64 ls_corr; /* correction to apply */
38+
int_fast64_t ls_corr; /* correction to apply */
3939
};
4040

4141
struct state

0 commit comments

Comments
 (0)