aboutsummaryrefslogtreecommitdiffstats
path: root/meta-oe/recipes-dbs/postgresql/files/0001-Sync-our-copy-of-the-timezone-library-with-IANA-rele.patch
blob: 6d8c46e748133f1ce166ea4b85ad306e9469a813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
From 12bfb778ce688fc662a6cb35f6298734fcf4856f Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 19 Oct 2018 19:36:34 -0400
Subject: [PATCH] Sync our copy of the timezone library with IANA release
 tzcode2018f.

About half of this is purely cosmetic changes to reduce the diff between
our code and theirs, like inserting "const" markers where they have them.

The other half is tracking actual code changes in zic.c and localtime.c.
I don't think any of these represent near-term compatibility hazards, but
it seems best to stay up to date.

I also fixed longstanding bugs in our code for producing the
known_abbrevs.txt list, which by chance hadn't been exposed before,
but which resulted in some garbage output after applying the upstream
changes in zic.c.  Notably, because upstream removed their old phony
transitions at the Big Bang, it's now necessary to cope with TZif files
containing no DST transition times at all.

Upstream-Status: Backport
Signed-off-by: Armin Kuster <akuster@mvista.com>

---
 src/timezone/README      |   2 +-
 src/timezone/localtime.c | 126 ++++++++++++------
 src/timezone/pgtz.h      |  10 +-
 src/timezone/private.h   |   3 +
 src/timezone/strftime.c  |  10 +-
 src/timezone/tzfile.h    |   7 +-
 src/timezone/zic.c       | 330 ++++++++++++++++++++++++++---------------------
 7 files changed, 291 insertions(+), 197 deletions(-)

Index: postgresql-10.5/src/timezone/README
===================================================================
--- postgresql-10.5.orig/src/timezone/README
+++ postgresql-10.5/src/timezone/README
@@ -55,7 +55,7 @@ match properly on the old version.
 Time Zone code
 ==============
 
-The code in this directory is currently synced with tzcode release 2018e.
+The code in this directory is currently synced with tzcode release 2018f.
 There are many cosmetic (and not so cosmetic) differences from the
 original tzcode library, but diffs in the upstream version should usually
 be propagated to our version.  Here are some notes about that.
Index: postgresql-10.5/src/timezone/localtime.c
===================================================================
--- postgresql-10.5.orig/src/timezone/localtime.c
+++ postgresql-10.5/src/timezone/localtime.c
@@ -1,3 +1,5 @@
+/* Convert timestamp from pg_time_t to struct pg_tm.  */
+
 /*
  * This file is in the public domain, so clarified as of
  * 1996-06-05 by Arthur David Olson.
@@ -117,7 +119,7 @@ init_ttinfo(struct ttinfo *s, int32 gmto
 }
 
 static int32
-detzcode(const char *codep)
+detzcode(const char *const codep)
 {
 	int32		result;
 	int			i;
@@ -143,7 +145,7 @@ detzcode(const char *codep)
 }
 
 static int64
-detzcode64(const char *codep)
+detzcode64(const char *const codep)
 {
 	uint64		result;
 	int			i;
@@ -258,8 +260,13 @@ tzloadbody(char const *name, char *canon
 		int32		charcnt = detzcode(up->tzhead.tzh_charcnt);
 		char const *p = up->buf + tzheadsize;
 
+		/*
+		 * Although tzfile(5) currently requires typecnt to be nonzero,
+		 * support future formats that may allow zero typecnt in files that
+		 * have a TZ string and no transitions.
+		 */
 		if (!(0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
-			  && 0 < typecnt && typecnt < TZ_MAX_TYPES
+			  && 0 <= typecnt && typecnt < TZ_MAX_TYPES
 			  && 0 <= timecnt && timecnt < TZ_MAX_TIMES
 			  && 0 <= charcnt && charcnt < TZ_MAX_CHARS
 			  && (ttisstdcnt == typecnt || ttisstdcnt == 0)
@@ -416,8 +423,7 @@ tzloadbody(char const *name, char *canon
 		struct state *ts = &lsp->u.st;
 
 		up->buf[nread - 1] = '\0';
-		if (tzparse(&up->buf[1], ts, false)
-			&& ts->typecnt == 2)
+		if (tzparse(&up->buf[1], ts, false))
 		{
 			/*
 			 * Attempt to reuse existing abbreviations. Without this,
@@ -430,7 +436,7 @@ tzloadbody(char const *name, char *canon
 			int			gotabbr = 0;
 			int			charcnt = sp->charcnt;
 
-			for (i = 0; i < 2; i++)
+			for (i = 0; i < ts->typecnt; i++)
 			{
 				char	   *tsabbr = ts->chars + ts->ttis[i].tt_abbrind;
 				int			j;
@@ -455,7 +461,7 @@ tzloadbody(char const *name, char *canon
 					}
 				}
 			}
-			if (gotabbr == 2)
+			if (gotabbr == ts->typecnt)
 			{
 				sp->charcnt = charcnt;
 
@@ -470,7 +476,8 @@ tzloadbody(char const *name, char *canon
 					sp->timecnt--;
 
 				for (i = 0; i < ts->timecnt; i++)
-					if (sp->ats[sp->timecnt - 1] < ts->ats[i])
+					if (sp->timecnt == 0
+						|| sp->ats[sp->timecnt - 1] < ts->ats[i])
 						break;
 				while (i < ts->timecnt
 					   && sp->timecnt < TZ_MAX_TIMES)
@@ -481,11 +488,13 @@ tzloadbody(char const *name, char *canon
 					sp->timecnt++;
 					i++;
 				}
-				sp->ttis[sp->typecnt++] = ts->ttis[0];
-				sp->ttis[sp->typecnt++] = ts->ttis[1];
+				for (i = 0; i < ts->typecnt; i++)
+					sp->ttis[sp->typecnt++] = ts->ttis[i];
 			}
 		}
 	}
+	if (sp->typecnt == 0)
+		return EINVAL;
 	if (sp->timecnt > 1)
 	{
 		for (i = 1; i < sp->timecnt; ++i)
@@ -507,6 +516,18 @@ tzloadbody(char const *name, char *canon
 	}
 
 	/*
+	 * Infer sp->defaulttype from the data.  Although this default type is
+	 * always zero for data from recent tzdb releases, things are trickier for
+	 * data from tzdb 2018e or earlier.
+	 *
+	 * The first set of heuristics work around bugs in 32-bit data generated
+	 * by tzdb 2013c or earlier.  The workaround is for zones like
+	 * Australia/Macquarie where timestamps before the first transition have a
+	 * time type that is not the earliest standard-time type.  See:
+	 * https://mm.icann.org/pipermail/tz/2013-May/019368.html
+	 */
+
+	/*
 	 * If type 0 is unused in transitions, it's the type to use for early
 	 * times.
 	 */
@@ -529,6 +550,11 @@ tzloadbody(char const *name, char *canon
 	}
 
 	/*
+	 * The next heuristics are for data generated by tzdb 2018e or earlier,
+	 * for zones like EST5EDT where the first transition is to DST.
+	 */
+
+	/*
 	 * If no result yet, find the first standard type. If there is none, punt
 	 * to type zero.
 	 */
@@ -542,7 +568,14 @@ tzloadbody(char const *name, char *canon
 				break;
 			}
 	}
+
+	/*
+	 * A simple 'sp->defaulttype = 0;' would suffice here if we didn't have to
+	 * worry about 2018e-or-earlier data.  Even simpler would be to remove the
+	 * defaulttype member and just use 0 in its place.
+	 */
 	sp->defaulttype = i;
+
 	return 0;
 }
 
@@ -601,10 +634,11 @@ static const int year_lengths[2] = {
 };
 
 /*
- * Given a pointer into a time zone string, scan until a character that is not
- * a valid character in a zone name is found. Return a pointer to that
- * character.
+ * Given a pointer into a timezone string, scan until a character that is not
+ * a valid character in a time zone abbreviation is found.
+ * Return a pointer to that character.
  */
+
 static const char *
 getzname(const char *strp)
 {
@@ -617,15 +651,17 @@ getzname(const char *strp)
 }
 
 /*
- * Given a pointer into an extended time zone string, scan until the ending
- * delimiter of the zone name is located. Return a pointer to the delimiter.
+ * Given a pointer into an extended timezone string, scan until the ending
+ * delimiter of the time zone abbreviation is located.
+ * Return a pointer to the delimiter.
  *
  * As with getzname above, the legal character set is actually quite
  * restricted, with other characters producing undefined results.
  * We don't do any checking here; checking is done later in common-case code.
  */
+
 static const char *
-getqzname(const char *strp, int delim)
+getqzname(const char *strp, const int delim)
 {
 	int			c;
 
@@ -635,13 +671,14 @@ getqzname(const char *strp, int delim)
 }
 
 /*
- * Given a pointer into a time zone string, extract a number from that string.
+ * Given a pointer into a timezone string, extract a number from that string.
  * Check that the number is within a specified range; if it is not, return
  * NULL.
  * Otherwise, return a pointer to the first character not part of the number.
  */
+
 static const char *
-getnum(const char *strp, int *nump, int min, int max)
+getnum(const char *strp, int *const nump, const int min, const int max)
 {
 	char		c;
 	int			num;
@@ -663,14 +700,15 @@ getnum(const char *strp, int *nump, int
 }
 
 /*
- * Given a pointer into a time zone string, extract a number of seconds,
+ * Given a pointer into a timezone string, extract a number of seconds,
  * in hh[:mm[:ss]] form, from the string.
  * If any error occurs, return NULL.
  * Otherwise, return a pointer to the first character not part of the number
  * of seconds.
  */
+
 static const char *
-getsecs(const char *strp, int32 *secsp)
+getsecs(const char *strp, int32 *const secsp)
 {
 	int			num;
 
@@ -704,13 +742,14 @@ getsecs(const char *strp, int32 *secsp)
 }
 
 /*
- * Given a pointer into a time zone string, extract an offset, in
+ * Given a pointer into a timezone string, extract an offset, in
  * [+-]hh[:mm[:ss]] form, from the string.
  * If any error occurs, return NULL.
  * Otherwise, return a pointer to the first character not part of the time.
  */
+
 static const char *
-getoffset(const char *strp, int32 *offsetp)
+getoffset(const char *strp, int32 *const offsetp)
 {
 	bool		neg = false;
 
@@ -730,13 +769,14 @@ getoffset(const char *strp, int32 *offse
 }
 
 /*
- * Given a pointer into a time zone string, extract a rule in the form
+ * Given a pointer into a timezone string, extract a rule in the form
  * date[/time]. See POSIX section 8 for the format of "date" and "time".
  * If a valid rule is not found, return NULL.
  * Otherwise, return a pointer to the first character not part of the rule.
  */
+
 static const char *
-getrule(const char *strp, struct rule *rulep)
+getrule(const char *strp, struct rule *const rulep)
 {
 	if (*strp == 'J')
 	{
@@ -795,9 +835,10 @@ getrule(const char *strp, struct rule *r
  * Given a year, a rule, and the offset from UT at the time that rule takes
  * effect, calculate the year-relative time that rule takes effect.
  */
+
 static int32
-transtime(int year, const struct rule *rulep,
-		  int32 offset)
+transtime(const int year, const struct rule *const rulep,
+		  const int32 offset)
 {
 	bool		leapyear;
 	int32		value;
@@ -981,7 +1022,7 @@ tzparse(const char *name, struct state *
 		{
 			dstname = name;
 			name = getzname(name);
-			dstlen = name - dstname;	/* length of DST zone name */
+			dstlen = name - dstname;	/* length of DST abbr. */
 		}
 		if (!dstlen)
 			return false;
@@ -1023,8 +1064,8 @@ tzparse(const char *name, struct state *
 			/*
 			 * Two transitions per year, from EPOCH_YEAR forward.
 			 */
-			init_ttinfo(&sp->ttis[0], -dstoffset, true, stdlen + 1);
-			init_ttinfo(&sp->ttis[1], -stdoffset, false, 0);
+			init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
+			init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
 			sp->defaulttype = 0;
 			timecnt = 0;
 			janfirst = 0;
@@ -1073,19 +1114,15 @@ tzparse(const char *name, struct state *
 					if (!increment_overflow_time
 						(&sp->ats[timecnt],
 						 janoffset + starttime))
-						sp->types[timecnt++] = reversed;
-					else if (janoffset)
-						sp->defaulttype = reversed;
+						sp->types[timecnt++] = !reversed;
 					sp->ats[timecnt] = janfirst;
 					if (!increment_overflow_time
 						(&sp->ats[timecnt],
 						 janoffset + endtime))
 					{
-						sp->types[timecnt++] = !reversed;
+						sp->types[timecnt++] = reversed;
 						yearlim = year + YEARSPERREPEAT + 1;
 					}
-					else if (janoffset)
-						sp->defaulttype = !reversed;
 				}
 				if (increment_overflow_time
 					(&janfirst, janoffset + yearsecs))
@@ -1094,7 +1131,10 @@ tzparse(const char *name, struct state *
 			}
 			sp->timecnt = timecnt;
 			if (!timecnt)
+			{
+				sp->ttis[0] = sp->ttis[1];
 				sp->typecnt = 1;	/* Perpetual DST.  */
+			}
 			else if (YEARSPERREPEAT < year - yearbeg)
 				sp->goback = sp->goahead = true;
 		}
@@ -1163,7 +1203,6 @@ tzparse(const char *name, struct state *
 					 * otherwise, add the standard time offset to the
 					 * transition time.
 					 */
-
 					/*
 					 * Transitions from DST to DDST will effectively disappear
 					 * since POSIX provides for only one DST offset.
@@ -1217,7 +1256,7 @@ tzparse(const char *name, struct state *
 }
 
 static void
-gmtload(struct state *sp)
+gmtload(struct state *const sp)
 {
 	if (tzload(gmt, NULL, sp, true) != 0)
 		tzparse(gmt, sp, true);
@@ -1232,7 +1271,7 @@ gmtload(struct state *sp)
  */
 static struct pg_tm *
 localsub(struct state const *sp, pg_time_t const *timep,
-		 struct pg_tm *tmp)
+		 struct pg_tm *const tmp)
 {
 	const struct ttinfo *ttisp;
 	int			i;
@@ -1300,6 +1339,11 @@ localsub(struct state const *sp, pg_time
 	}
 	ttisp = &sp->ttis[i];
 
+	/*
+	 * To get (wrong) behavior that's compatible with System V Release 2.0
+	 * you'd replace the statement below with t += ttisp->tt_gmtoff;
+	 * timesub(&t, 0L, sp, tmp);
+	 */
 	result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
 	if (result)
 	{
@@ -1322,8 +1366,10 @@ pg_localtime(const pg_time_t *timep, con
  *
  * Except we have a private "struct state" for GMT, so no sp is passed in.
  */
+
 static struct pg_tm *
-gmtsub(pg_time_t const *timep, int32 offset, struct pg_tm *tmp)
+gmtsub(pg_time_t const *timep, int32 offset,
+	   struct pg_tm *tmp)
 {
 	struct pg_tm *result;
 
@@ -1337,6 +1383,7 @@ gmtsub(pg_time_t const *timep, int32 off
 		gmt_is_set = true;
 		gmtload(gmtptr);
 	}
+
 	result = timesub(timep, offset, gmtptr, tmp);
 
 	/*
@@ -1361,6 +1408,7 @@ pg_gmtime(const pg_time_t *timep)
  * Return the number of leap years through the end of the given year
  * where, to make the math easy, the answer for year zero is defined as zero.
  */
+
 static int
 leaps_thru_end_of_nonneg(int y)
 {
Index: postgresql-10.5/src/timezone/pgtz.h
===================================================================
--- postgresql-10.5.orig/src/timezone/pgtz.h
+++ postgresql-10.5/src/timezone/pgtz.h
@@ -49,10 +49,16 @@ struct state
 	pg_time_t	ats[TZ_MAX_TIMES];
 	unsigned char types[TZ_MAX_TIMES];
 	struct ttinfo ttis[TZ_MAX_TYPES];
-	char		chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, 3 /* sizeof gmt */ ),
+	char		chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, 4 /* sizeof gmt */ ),
 							  (2 * (TZ_STRLEN_MAX + 1)))];
 	struct lsinfo lsis[TZ_MAX_LEAPS];
-	int			defaulttype;	/* for early times or if no transitions */
+
+	/*
+	 * The time type to use for early times or if no transitions. It is always
+	 * zero for recent tzdb releases. It might be nonzero for data from tzdb
+	 * 2018e or earlier.
+	 */
+	int			defaulttype;
 };
 
 
Index: postgresql-10.5/src/timezone/private.h
===================================================================
--- postgresql-10.5.orig/src/timezone/private.h
+++ postgresql-10.5/src/timezone/private.h
@@ -1,4 +1,7 @@
+/* Private header for tzdb code.  */
+
 #ifndef PRIVATE_H
+
 #define PRIVATE_H
 
 /*
Index: postgresql-10.5/src/timezone/strftime.c
===================================================================
--- postgresql-10.5.orig/src/timezone/strftime.c
+++ postgresql-10.5/src/timezone/strftime.c
@@ -1,4 +1,4 @@
-/* Convert a broken-down timestamp to a string. */
+/* Convert a broken-down timestamp to a string.  */
 
 /*
  * Copyright 1989 The Regents of the University of California.
@@ -115,7 +115,7 @@ static char *_add(const char *, char *,
 static char *_conv(int, const char *, char *, const char *);
 static char *_fmt(const char *, const struct pg_tm *, char *, const char *,
 	 enum warn *);
-static char *_yconv(int, int, bool, bool, char *, const char *);
+static char *_yconv(int, int, bool, bool, char *, char const *);
 
 
 size_t
@@ -441,7 +441,8 @@ _fmt(const char *format, const struct pg
 
 					/*
 					 * C99 and later say that %Z must be replaced by the empty
-					 * string if the time zone is not determinable.
+					 * string if the time zone abbreviation is not
+					 * determinable.
 					 */
 					continue;
 				case 'z':
@@ -519,6 +520,7 @@ _add(const char *str, char *pt, const ch
  * same output as %Y, and that %Y contains at least 4 bytes,
  * with more only if necessary.
  */
+
 static char *
 _yconv(int a, int b, bool convert_top, bool convert_yy,
 	   char *pt, const char *ptlim)
@@ -526,7 +528,7 @@ _yconv(int a, int b, bool convert_top, b
 	int			lead;
 	int			trail;
 
-#define DIVISOR 100
+#define DIVISOR	100
 	trail = a % DIVISOR + b % DIVISOR;
 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
 	trail %= DIVISOR;
Index: postgresql-10.5/src/timezone/tzfile.h
===================================================================
--- postgresql-10.5.orig/src/timezone/tzfile.h
+++ postgresql-10.5/src/timezone/tzfile.h
@@ -1,4 +1,7 @@
+/* Layout and location of TZif files.  */
+
 #ifndef TZFILE_H
+
 #define TZFILE_H
 
 /*
@@ -21,14 +24,14 @@
  * Information about time zone files.
  */
 
-#define TZDEFAULT	"localtime"
+#define TZDEFAULT	"/etc/localtime"
 #define TZDEFRULES	"posixrules"
 
 /*
  * Each file begins with. . .
  */
 
-#define TZ_MAGIC	"TZif"
+#define	TZ_MAGIC	"TZif"
 
 struct tzhead
 {
Index: postgresql-10.5/src/timezone/zic.c
===================================================================
--- postgresql-10.5.orig/src/timezone/zic.c
+++ postgresql-10.5/src/timezone/zic.c
@@ -1,3 +1,5 @@
+/* Compile .zi time zone data into TZif binary files.  */
+
 /*
  * This file is in the public domain, so clarified as of
  * 2006-07-17 by Arthur David Olson.
@@ -130,8 +132,7 @@ static void adjleap(void);
 static void associate(void);
 static void dolink(const char *, const char *, bool);
 static char **getfields(char *buf);
-static zic_t gethms(const char *string, const char *errstring,
-	   bool);
+static zic_t gethms(const char *string, const char *errstring);
 static zic_t getstdoff(char *, bool *);
 static void infile(const char *filename);
 static void inleap(char **fields, int nfields);
@@ -162,7 +163,7 @@ enum
 PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1};
 
 /* If true, work around a bug in Qt 5.6.1 and earlier, which mishandles
-   tz binary files whose POSIX-TZ-style strings contain '<'; see
+   TZif files whose POSIX-TZ-style strings contain '<'; see
    QTBUG-53071 <https://bugreports.qt.io/browse/QTBUG-53071>.  This
    workaround will no longer be needed when Qt 5.6.1 and earlier are
    obsolete, say in the year 2021.  */
@@ -211,7 +212,7 @@ static int	typecnt;
 #define ZF_RULE		3
 #define ZF_FORMAT	4
 #define ZF_TILYEAR	5
-#define ZF_TILMONTH 6
+#define ZF_TILMONTH	6
 #define ZF_TILDAY	7
 #define ZF_TILTIME	8
 #define ZONE_MINFIELDS	5
@@ -224,12 +225,12 @@ static int	typecnt;
 #define ZFC_GMTOFF	0
 #define ZFC_RULE	1
 #define ZFC_FORMAT	2
-#define ZFC_TILYEAR 3
+#define ZFC_TILYEAR	3
 #define ZFC_TILMONTH	4
 #define ZFC_TILDAY	5
-#define ZFC_TILTIME 6
-#define ZONEC_MINFIELDS 3
-#define ZONEC_MAXFIELDS 7
+#define ZFC_TILTIME	6
+#define ZONEC_MINFIELDS	3
+#define ZONEC_MAXFIELDS	7
 
 /*
  * Which files are which on a Rule line.
@@ -244,7 +245,7 @@ static int	typecnt;
 #define RF_TOD		7
 #define RF_STDOFF	8
 #define RF_ABBRVAR	9
-#define RULE_FIELDS 10
+#define RULE_FIELDS	10
 
 /*
  * Which fields are which on a Link line.
@@ -252,7 +253,7 @@ static int	typecnt;
 
 #define LF_FROM		1
 #define LF_TO		2
-#define LINK_FIELDS 3
+#define LINK_FIELDS	3
 
 /*
  * Which fields are which on a Leap line.
@@ -264,7 +265,7 @@ static int	typecnt;
 #define LP_TIME		4
 #define LP_CORR		5
 #define LP_ROLL		6
-#define LEAP_FIELDS 7
+#define LEAP_FIELDS	7
 
 /*
  * Year synonyms.
@@ -998,48 +999,6 @@ dolink(char const *fromfield, char const
 static zic_t const min_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE);
 static zic_t const max_time = MAXVAL(zic_t, TIME_T_BITS_IN_FILE);
 
-/*
- * Estimated time of the Big Bang, in seconds since the POSIX epoch.
- * rounded downward to the negation of a power of two that is
- * comfortably outside the error bounds.
- *
- * For the time of the Big Bang, see:
- *
- * Ade PAR, Aghanim N, Armitage-Caplan C et al.  Planck 2013 results.
- * I. Overview of products and scientific results.
- * arXiv:1303.5062 2013-03-20 20:10:01 UTC
- * <https://arxiv.org/pdf/1303.5062v1> [PDF]
- *
- * Page 36, Table 9, row Age/Gyr, column Planck+WP+highL+BAO 68% limits
- * gives the value 13.798 plus-or-minus 0.037 billion years.
- * Multiplying this by 1000000000 and then by 31557600 (the number of
- * seconds in an astronomical year) gives a value that is comfortably
- * less than 2**59, so BIG_BANG is - 2**59.
- *
- * BIG_BANG is approximate, and may change in future versions.
- * Please do not rely on its exact value.
- */
-
-#ifndef BIG_BANG
-#define BIG_BANG (- (((zic_t) 1) << 59))
-#endif
-
-/* If true, work around GNOME bug 730332
-   <https://bugzilla.gnome.org/show_bug.cgi?id=730332>
-   by refusing to output time stamps before BIG_BANG.
-   Such time stamps are physically suspect anyway.
-
-   The GNOME bug is scheduled to be fixed in GNOME 3.22, and if so
-   this workaround will no longer be needed when GNOME 3.21 and
-   earlier are obsolete, say in the year 2021.  */
-enum
-{
-WORK_AROUND_GNOME_BUG_730332 = true};
-
-static const zic_t early_time = (WORK_AROUND_GNOME_BUG_730332
-								 ? BIG_BANG
-								 : MINVAL(zic_t, TIME_T_BITS_IN_FILE));
-
 /* Return true if NAME is a directory.  */
 static bool
 itsdir(char const *name)
@@ -1281,8 +1240,9 @@ infile(const char *name)
  * A null string maps to zero.
  * Call error with errstring and return zero on errors.
  */
+
 static zic_t
-gethms(char const *string, char const *errstring, bool signable)
+gethms(char const *string, char const *errstring)
 {
 	/* PG: make hh be int not zic_t to avoid sscanf portability issues */
 	int			hh;
@@ -1299,9 +1259,7 @@ gethms(char const *string, char const *e
 
 	if (string == NULL || *string == '\0')
 		return 0;
-	if (!signable)
-		sign = 1;
-	else if (*string == '-')
+	if (*string == '-')
 	{
 		sign = -1;
 		++string;
@@ -1384,7 +1342,7 @@ getstdoff(char *field, bool *isdst)
 				break;
 		}
 	}
-	stdoff = gethms(field, _("invalid saved time"), true);
+	stdoff = gethms(field, _("invalid saved time"));
 	*isdst = dst < 0 ? stdoff != 0 : dst;
 	return stdoff;
 }
@@ -1399,10 +1357,29 @@ inrule(char **fields, int nfields)
 		error(_("wrong number of fields on Rule line"));
 		return;
 	}
-	if (*fields[RF_NAME] == '\0')
+	switch (*fields[RF_NAME])
 	{
-		error(_("nameless rule"));
-		return;
+		case '\0':
+		case ' ':
+		case '\f':
+		case '\n':
+		case '\r':
+		case '\t':
+		case '\v':
+		case '+':
+		case '-':
+		case '0':
+		case '1':
+		case '2':
+		case '3':
+		case '4':
+		case '5':
+		case '6':
+		case '7':
+		case '8':
+		case '9':
+			error(_("Invalid rule name \"%s\""), fields[RF_NAME]);
+			return;
 	}
 	r.r_filename = filename;
 	r.r_linenum = linenum;
@@ -1507,7 +1484,7 @@ inzsub(char **fields, int nfields, bool
 	}
 	z.z_filename = filename;
 	z.z_linenum = linenum;
-	z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"), true);
+	z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"));
 	if ((cp = strchr(fields[i_format], '%')) != NULL)
 	{
 		if ((*++cp != 's' && *cp != 'z') || strchr(cp, '%')
@@ -1649,7 +1626,7 @@ inleap(char **fields, int nfields)
 		return;
 	}
 	t = dayoff * SECSPERDAY;
-	tod = gethms(fields[LP_TIME], _("invalid time of day"), false);
+	tod = gethms(fields[LP_TIME], _("invalid time of day"));
 	cp = fields[LP_CORR];
 	{
 		bool		positive;
@@ -1757,7 +1734,7 @@ rulesub(struct rule *rp, const char *loy
 				break;
 		}
 	}
-	rp->r_tod = gethms(dp, _("invalid time of day"), false);
+	rp->r_tod = gethms(dp, _("invalid time of day"));
 	free(dp);
 
 	/*
@@ -1942,7 +1919,43 @@ is32(const zic_t x)
 }
 
 static void
-writezone(const char *const name, const char *const string, char version)
+swaptypes(int i, int j)
+{
+	{
+		zic_t		t = gmtoffs[i];
+
+		gmtoffs[i] = gmtoffs[j];
+		gmtoffs[j] = t;
+	}
+	{
+		char		t = isdsts[i];
+
+		isdsts[i] = isdsts[j];
+		isdsts[j] = t;
+	}
+	{
+		unsigned char t = abbrinds[i];
+
+		abbrinds[i] = abbrinds[j];
+		abbrinds[j] = t;
+	}
+	{
+		bool		t = ttisstds[i];
+
+		ttisstds[i] = ttisstds[j];
+		ttisstds[j] = t;
+	}
+	{
+		bool		t = ttisgmts[i];
+
+		ttisgmts[i] = ttisgmts[j];
+		ttisgmts[j] = t;
+	}
+}
+
+static void
+writezone(const char *const name, const char *const string, char version,
+		  int defaulttype)
 {
 	FILE	   *fp;
 	ptrdiff_t	i,
@@ -1977,14 +1990,12 @@ writezone(const char *const name, const
 
 		toi = 0;
 		fromi = 0;
-		while (fromi < timecnt && attypes[fromi].at < early_time)
-			++fromi;
 		for (; fromi < timecnt; ++fromi)
 		{
-			if (toi > 1 && ((attypes[fromi].at +
-							 gmtoffs[attypes[toi - 1].type]) <=
-							(attypes[toi - 1].at +
-							 gmtoffs[attypes[toi - 2].type])))
+			if (toi != 0 && ((attypes[fromi].at +
+							  gmtoffs[attypes[toi - 1].type]) <=
+							 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
+															: attypes[toi - 2].type])))
 			{
 				attypes[toi - 1].type =
 					attypes[fromi].type;
@@ -2019,8 +2030,8 @@ writezone(const char *const name, const
 	}
 
 	/*
-	 * Work around QTBUG-53071 for time stamps less than y2038_boundary - 1,
-	 * by inserting a no-op transition at time y2038_boundary - 1. This works
+	 * Work around QTBUG-53071 for timestamps less than y2038_boundary - 1, by
+	 * inserting a no-op transition at time y2038_boundary - 1. This works
 	 * only for timestamps before the boundary, which should be good enough in
 	 * practice as QTBUG-53071 should be long-dead by 2038.
 	 */
@@ -2116,7 +2127,8 @@ writezone(const char *const name, const
 		int			thisleapi,
 					thisleapcnt,
 					thisleaplim;
-		int			writetype[TZ_MAX_TYPES];
+		int			old0;
+		char		omittype[TZ_MAX_TYPES];
 		int			typemap[TZ_MAX_TYPES];
 		int			thistypecnt;
 		char		thischars[TZ_MAX_CHARS];
@@ -2144,28 +2156,19 @@ writezone(const char *const name, const
 			error(_("too many transition times"));
 		thistimelim = thistimei + thistimecnt;
 		thisleaplim = thisleapi + thisleapcnt;
-		for (i = 0; i < typecnt; ++i)
-			writetype[i] = thistimecnt == timecnt;
-		if (thistimecnt == 0)
-		{
-			/*
-			 * No transition times fall in the current (32- or 64-bit) window.
-			 */
-			if (typecnt != 0)
-				writetype[typecnt - 1] = true;
-		}
-		else
-		{
-			for (i = thistimei - 1; i < thistimelim; ++i)
-				if (i >= 0)
-					writetype[types[i]] = true;
+		memset(omittype, true, typecnt);
+		omittype[defaulttype] = false;
+		for (i = thistimei; i < thistimelim; i++)
+			omittype[types[i]] = false;
+
+		/*
+		 * Reorder types to make DEFAULTTYPE type 0. Use TYPEMAP to swap OLD0
+		 * and DEFAULTTYPE so that DEFAULTTYPE appears as type 0 in the output
+		 * instead of OLD0.  TYPEMAP also omits unused types.
+		 */
+		old0 = strlen(omittype);
+		swaptypes(old0, defaulttype);
 
-			/*
-			 * For America/Godthab and Antarctica/Palmer
-			 */
-			if (thistimei == 0)
-				writetype[0] = true;
-		}
 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
 
 		/*
@@ -2187,8 +2190,8 @@ writezone(const char *const name, const
 					mrudst = types[i];
 				else
 					mrustd = types[i];
-			for (i = 0; i < typecnt; ++i)
-				if (writetype[i])
+			for (i = old0; i < typecnt; i++)
+				if (!omittype[i])
 				{
 					if (isdsts[i])
 						hidst = i;
@@ -2205,7 +2208,7 @@ writezone(const char *const name, const
 							   ttisstds[mrudst],
 							   ttisgmts[mrudst]);
 				isdsts[mrudst] = 1;
-				writetype[type] = true;
+				omittype[type] = false;
 			}
 			if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
 				gmtoffs[histd] != gmtoffs[mrustd])
@@ -2217,22 +2220,26 @@ writezone(const char *const name, const
 							   ttisstds[mrustd],
 							   ttisgmts[mrustd]);
 				isdsts[mrustd] = 0;
-				writetype[type] = true;
+				omittype[type] = false;
 			}
 		}
 #endif							/* !defined
 								 * LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
 		thistypecnt = 0;
-		for (i = 0; i < typecnt; ++i)
-			typemap[i] = writetype[i] ? thistypecnt++ : -1;
+		for (i = old0; i < typecnt; i++)
+			if (!omittype[i])
+				typemap[i == old0 ? defaulttype
+						: i == defaulttype ? old0 : i]
+					= thistypecnt++;
+
 		for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
 			indmap[i] = -1;
 		thischarcnt = 0;
-		for (i = 0; i < typecnt; ++i)
+		for (i = old0; i < typecnt; i++)
 		{
 			char	   *thisabbr;
 
-			if (!writetype[i])
+			if (omittype[i])
 				continue;
 			if (indmap[abbrinds[i]] >= 0)
 				continue;
@@ -2267,23 +2274,16 @@ writezone(const char *const name, const
 		DO(tzh_typecnt);
 		DO(tzh_charcnt);
 #undef DO
-		for (i = thistimei; i < thistimelim; ++i)
-			if (pass == 1)
 
-				/*
-				 * Output an INT32_MIN "transition" if appropriate; see above.
-				 */
-				puttzcode(((ats[i] < PG_INT32_MIN) ?
-						   PG_INT32_MIN : ats[i]), fp);
-			else
+		/* PG: print current timezone abbreviations if requested */
+		if (print_abbrevs && pass == 2)
+		{
+			/* Print "type" data for periods ending after print_cutoff */
+			for (i = thistimei; i < thistimelim; ++i)
 			{
-				puttzcode64(ats[i], fp);
-
-				/* Print current timezone abbreviations if requested */
-				if (print_abbrevs &&
-					(i == thistimelim - 1 || ats[i + 1] > print_cutoff))
+				if (i == thistimelim - 1 || ats[i + 1] > print_cutoff)
 				{
-					unsigned char tm = typemap[types[i]];
+					unsigned char tm = types[i];
 					char	   *thisabbrev = &thischars[indmap[abbrinds[tm]]];
 
 					/* filter out assorted junk entries */
@@ -2295,6 +2295,32 @@ writezone(const char *const name, const
 								isdsts[tm] ? "\tD" : "");
 				}
 			}
+			/* Print the default type if we have no transitions at all */
+			if (thistimei >= thistimelim)
+			{
+				unsigned char tm = defaulttype;
+				char	   *thisabbrev = &thischars[indmap[abbrinds[tm]]];
+
+				/* filter out assorted junk entries */
+				if (strcmp(thisabbrev, GRANDPARENTED) != 0 &&
+					strcmp(thisabbrev, "zzz") != 0)
+					fprintf(stdout, "%s\t" INT64_FORMAT "%s\n",
+							thisabbrev,
+							gmtoffs[tm],
+							isdsts[tm] ? "\tD" : "");
+			}
+		}
+
+		for (i = thistimei; i < thistimelim; ++i)
+			if (pass == 1)
+
+				/*
+				 * Output an INT32_MIN "transition" if appropriate; see above.
+				 */
+				puttzcode(((ats[i] < PG_INT32_MIN) ?
+						   PG_INT32_MIN : ats[i]), fp);
+			else
+				puttzcode64(ats[i], fp);
 		for (i = thistimei; i < thistimelim; ++i)
 		{
 			unsigned char uc;
@@ -2302,8 +2328,8 @@ writezone(const char *const name, const
 			uc = typemap[types[i]];
 			fwrite(&uc, sizeof uc, 1, fp);
 		}
-		for (i = 0; i < typecnt; ++i)
-			if (writetype[i])
+		for (i = old0; i < typecnt; i++)
+			if (!omittype[i])
 			{
 				puttzcode(gmtoffs[i], fp);
 				putc(isdsts[i], fp);
@@ -2346,12 +2372,13 @@ writezone(const char *const name, const
 				puttzcode64(todo, fp);
 			puttzcode(corr[i], fp);
 		}
-		for (i = 0; i < typecnt; ++i)
-			if (writetype[i])
+		for (i = old0; i < typecnt; i++)
+			if (!omittype[i])
 				putc(ttisstds[i], fp);
-		for (i = 0; i < typecnt; ++i)
-			if (writetype[i])
+		for (i = old0; i < typecnt; i++)
+			if (!omittype[i])
 				putc(ttisgmts[i], fp);
+		swaptypes(old0, defaulttype);
 	}
 	fprintf(fp, "\n%s\n", string);
 	close_file(fp, directory, name);
@@ -2757,6 +2784,7 @@ outzone(const struct zone *zpfirst, ptrd
 	zic_t		one = 1;
 	zic_t		y2038_boundary = one << 31;
 	zic_t		max_year0;
+	int			defaulttype = -1;
 
 	max_abbr_len = 2 + max_format_len + max_abbrvar_len;
 	max_envvar_len = 2 * max_abbr_len + 5 * 9;
@@ -2880,9 +2908,9 @@ outzone(const struct zone *zpfirst, ptrd
 		 */
 		stdoff = 0;
 		zp = &zpfirst[i];
-		usestart = i > 0 && (zp - 1)->z_untiltime > early_time;
+		usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
 		useuntil = i < (zonecount - 1);
-		if (useuntil && zp->z_untiltime <= early_time)
+		if (useuntil && zp->z_untiltime <= min_time)
 			continue;
 		gmtoff = zp->z_gmtoff;
 		eat(zp->z_filename, zp->z_linenum);
@@ -2901,7 +2929,7 @@ outzone(const struct zone *zpfirst, ptrd
 				usestart = false;
 			}
 			else
-				addtt(early_time, type);
+				defaulttype = type;
 		}
 		else
 			for (year = min_year; year <= max_year; ++year)
@@ -3032,6 +3060,8 @@ outzone(const struct zone *zpfirst, ptrd
 					offset = oadd(zp->z_gmtoff, rp->r_stdoff);
 					type = addtype(offset, ab, rp->r_isdst,
 								   rp->r_todisstd, rp->r_todisgmt);
+					if (defaulttype < 0 && !rp->r_isdst)
+						defaulttype = type;
 					if (rp->r_hiyear == ZIC_MAX
 						&& !(0 <= lastatmax
 							 && ktime < attypes[lastatmax].at))
@@ -3050,11 +3080,15 @@ outzone(const struct zone *zpfirst, ptrd
 			if (*startbuf == '\0')
 				error(_("cannot determine time zone abbreviation to use just after until time"));
 			else
-				addtt(starttime,
-					  addtype(startoff, startbuf,
-							  startoff != zp->z_gmtoff,
-							  startttisstd,
-							  startttisgmt));
+			{
+				bool		isdst = startoff != zp->z_gmtoff;
+
+				type = addtype(startoff, startbuf, isdst,
+							   startttisstd, startttisgmt);
+				if (defaulttype < 0 && !isdst)
+					defaulttype = type;
+				addtt(starttime, type);
+			}
 		}
 
 		/*
@@ -3071,6 +3105,8 @@ outzone(const struct zone *zpfirst, ptrd
 				starttime = tadd(starttime, -gmtoff);
 		}
 	}
+	if (defaulttype < 0)
+		defaulttype = 0;
 	if (0 <= lastatmax)
 		attypes[lastatmax].dontmerge = true;
 	if (do_extend)
@@ -3100,7 +3136,7 @@ outzone(const struct zone *zpfirst, ptrd
 			attypes[timecnt - 1].dontmerge = true;
 		}
 	}
-	writezone(zpfirst->z_name, envvar, version);
+	writezone(zpfirst->z_name, envvar, version, defaulttype);
 	free(startbuf);
 	free(ab);
 	free(envvar);
@@ -3109,21 +3145,6 @@ outzone(const struct zone *zpfirst, ptrd
 static void
 addtt(zic_t starttime, int type)
 {
-	if (starttime <= early_time
-		|| (timecnt == 1 && attypes[0].at < early_time))
-	{
-		gmtoffs[0] = gmtoffs[type];
-		isdsts[0] = isdsts[type];
-		ttisstds[0] = ttisstds[type];
-		ttisgmts[0] = ttisgmts[type];
-		if (abbrinds[type] != 0)
-			strcpy(chars, &chars[abbrinds[type]]);
-		abbrinds[0] = 0;
-		charcnt = strlen(chars) + 1;
-		typecnt = 1;
-		timecnt = 0;
-		type = 0;
-	}
 	attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
 	attypes[timecnt].at = starttime;
 	attypes[timecnt].dontmerge = false;
@@ -3361,7 +3382,7 @@ is_alpha(char a)
 }
 
 /* If A is an uppercase character in the C locale, return its lowercase
- * counterpart.  Otherwise, return A.  */
+   counterpart.  Otherwise, return A.  */
 static char
 lowerit(char a)
 {
@@ -3628,6 +3649,18 @@ rpytime(const struct rule *rp, zic_t wan
 	dayoff = 0;
 	m = TM_JANUARY;
 	y = EPOCH_YEAR;
+	if (y < wantedy)
+	{
+		wantedy -= y;
+		dayoff = (wantedy / YEARSPERREPEAT) * (SECSPERREPEAT / SECSPERDAY);
+		wantedy %= YEARSPERREPEAT;
+		wantedy += y;
+	}
+	else if (wantedy < 0)
+	{
+		dayoff = (wantedy / YEARSPERREPEAT) * (SECSPERREPEAT / SECSPERDAY);
+		wantedy %= YEARSPERREPEAT;
+	}
 	while (wantedy != y)
 	{
 		if (wantedy > y)
@@ -3706,7 +3739,6 @@ will not work with pre-2004 versions of
 	if (dayoff > max_time / SECSPERDAY)
 		return max_time;
 	t = (zic_t) dayoff * SECSPERDAY;
-
 	return tadd(t, rp->r_tod);
 }