26
26
#ifndef WILDABBR
27
27
/*
28
28
* Someone might make incorrect use of a time zone abbreviation:
29
- * 1. They might reference tzname[0] before calling tzset (explicitly
29
+ * 1. They might reference tzname[0] before calling tzset (explicitly
30
30
* or implicitly).
31
- * 2. They might reference tzname[1] before calling tzset (explicitly
31
+ * 2. They might reference tzname[1] before calling tzset (explicitly
32
32
* or implicitly).
33
- * 3. They might reference tzname[1] after setting to a time zone
33
+ * 3. They might reference tzname[1] after setting to a time zone
34
34
* in which Daylight Saving Time is never observed.
35
- * 4. They might reference tzname[0] after setting to a time zone
35
+ * 4. They might reference tzname[0] after setting to a time zone
36
36
* in which Standard Time is never observed.
37
- * 5. They might reference tm.TM_ZONE after calling offtime.
37
+ * 5. They might reference tm.TM_ZONE after calling offtime.
38
38
* What's best to do in the above cases is open to debate;
39
39
* for now, we just set things up so that in any of the five cases
40
40
* WILDABBR is used. Another possibility: initialize tzname[0] to the
44
44
* that tzname[0] has the "normal" length of three characters).
45
45
*/
46
46
#define WILDABBR " "
47
- #endif /* !defined WILDABBR */
47
+ #endif /* !defined WILDABBR */
48
48
49
49
static const char wildabbr [] = WILDABBR ;
50
50
51
51
static const char gmt [] = "GMT" ;
52
52
53
- /* The minimum and maximum finite time values. This assumes no padding. */
54
- static const pg_time_t time_t_min = MINVAL (pg_time_t , TYPE_BIT (pg_time_t ));
55
- static const pg_time_t time_t_max = MAXVAL (pg_time_t , TYPE_BIT (pg_time_t ));
56
-
57
53
/*
58
- * We cache the result of trying to load the TZDEFRULES zone here.
54
+ * PG: We cache the result of trying to load the TZDEFRULES zone here.
59
55
* tzdefrules_loaded is 0 if not tried yet, +1 if good, -1 if failed.
60
56
*/
61
57
static struct state tzdefrules_s ;
62
58
static int tzdefrules_loaded = 0 ;
63
59
64
60
/*
65
61
* The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
66
- * We default to US rules as of 1999-08-17 .
62
+ * Default to US rules as of 2017-05-07 .
67
63
* POSIX 1003.1 section 8.1.1 says that the default DST rules are
68
64
* implementation dependent; for historical reasons, US rules are a
69
65
* common default.
70
66
*/
71
- #define TZDEFRULESTRING ",M4.1 .0,M10.5 .0"
67
+ #define TZDEFRULESTRING ",M3.2 .0,M11.1 .0"
72
68
73
69
/* structs ttinfo, lsinfo, state have been moved to pgtz.h */
74
70
@@ -112,7 +108,7 @@ static struct pg_tm tm;
112
108
113
109
/* Initialize *S to a value based on GMTOFF, ISDST, and ABBRIND. */
114
110
static void
115
- init_ttinfo (struct ttinfo * s , int32 gmtoff , bool isdst , int abbrind )
111
+ init_ttinfo (struct ttinfo * s , int32 gmtoff , bool isdst , int abbrind )
116
112
{
117
113
s -> tt_gmtoff = gmtoff ;
118
114
s -> tt_isdst = isdst ;
@@ -189,23 +185,23 @@ union input_buffer
189
185
190
186
/* The entire buffer. */
191
187
char buf [2 * sizeof (struct tzhead ) + 2 * sizeof (struct state )
192
- + 4 * TZ_MAX_TIMES ];
188
+ + 4 * TZ_MAX_TIMES ];
193
189
};
194
190
195
191
/* Local storage needed for 'tzloadbody'. */
196
192
union local_storage
197
193
{
198
- /* We don't need the "fullname" member */
199
-
200
194
/* The results of analyzing the file's contents after it is opened. */
201
- struct
195
+ struct file_analysis
202
196
{
203
197
/* The input buffer. */
204
198
union input_buffer u ;
205
199
206
200
/* A temporary state used for parsing a TZ string in the file. */
207
201
struct state st ;
208
202
} u ;
203
+
204
+ /* We don't need the "fullname" member */
209
205
};
210
206
211
207
/* Load tz data from the file named NAME into *SP. Read extended
@@ -215,8 +211,8 @@ union local_storage
215
211
* given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
216
212
*/
217
213
static int
218
- tzloadbody (char const * name , char * canonname , struct state * sp , bool doextend ,
219
- union local_storage * lsp )
214
+ tzloadbody (char const * name , char * canonname , struct state * sp , bool doextend ,
215
+ union local_storage * lsp )
220
216
{
221
217
int i ;
222
218
int fid ;
@@ -255,6 +251,8 @@ tzloadbody(char const * name, char *canonname, struct state * sp, bool doextend,
255
251
{
256
252
int32 ttisstdcnt = detzcode (up -> tzhead .tzh_ttisstdcnt );
257
253
int32 ttisgmtcnt = detzcode (up -> tzhead .tzh_ttisgmtcnt );
254
+ int64 prevtr = 0 ;
255
+ int32 prevcorr = 0 ;
258
256
int32 leapcnt = detzcode (up -> tzhead .tzh_leapcnt );
259
257
int32 timecnt = detzcode (up -> tzhead .tzh_timecnt );
260
258
int32 typecnt = detzcode (up -> tzhead .tzh_typecnt );
@@ -270,7 +268,7 @@ tzloadbody(char const * name, char *canonname, struct state * sp, bool doextend,
270
268
return EINVAL ;
271
269
if (nread
272
270
< (tzheadsize /* struct tzhead */
273
- + timecnt * stored /* ats */
271
+ + timecnt * stored /* ats */
274
272
+ timecnt /* types */
275
273
+ typecnt * 6 /* ttinfos */
276
274
+ charcnt /* chars */
@@ -285,21 +283,21 @@ tzloadbody(char const * name, char *canonname, struct state * sp, bool doextend,
285
283
286
284
/*
287
285
* Read transitions, discarding those out of pg_time_t range. But
288
- * pretend the last transition before time_t_min occurred at
289
- * time_t_min .
286
+ * pretend the last transition before TIME_T_MIN occurred at
287
+ * TIME_T_MIN .
290
288
*/
291
289
timecnt = 0 ;
292
290
for (i = 0 ; i < sp -> timecnt ; ++ i )
293
291
{
294
292
int64 at
295
293
= stored == 4 ? detzcode (p ) : detzcode64 (p );
296
294
297
- sp -> types [i ] = at <= time_t_max ;
295
+ sp -> types [i ] = at <= TIME_T_MAX ;
298
296
if (sp -> types [i ])
299
297
{
300
298
pg_time_t attime
301
- = ((TYPE_SIGNED (pg_time_t ) ? at < time_t_min : at < 0 )
302
- ? time_t_min : at );
299
+ = ((TYPE_SIGNED (pg_time_t ) ? at < TIME_T_MIN : at < 0 )
300
+ ? TIME_T_MIN : at );
303
301
304
302
if (timecnt && attime <= sp -> ats [timecnt - 1 ])
305
303
{
@@ -354,20 +352,22 @@ tzloadbody(char const * name, char *canonname, struct state * sp, bool doextend,
354
352
int32 corr = detzcode (p + stored );
355
353
356
354
p += stored + 4 ;
357
- if (tr <= time_t_max )
355
+ /* Leap seconds cannot occur before the Epoch. */
356
+ if (tr < 0 )
357
+ return EINVAL ;
358
+ if (tr <= TIME_T_MAX )
358
359
{
359
- pg_time_t trans
360
- = ((TYPE_SIGNED (pg_time_t ) ? tr < time_t_min : tr < 0 )
361
- ? time_t_min : tr );
362
-
363
- if (leapcnt && trans <= sp -> lsis [leapcnt - 1 ].ls_trans )
364
- {
365
- if (trans < sp -> lsis [leapcnt - 1 ].ls_trans )
366
- return EINVAL ;
367
- leapcnt -- ;
368
- }
369
- sp -> lsis [leapcnt ].ls_trans = trans ;
370
- sp -> lsis [leapcnt ].ls_corr = corr ;
360
+ /*
361
+ * Leap seconds cannot occur more than once per UTC month, and
362
+ * UTC months are at least 28 days long (minus 1 second for a
363
+ * negative leap second). Each leap second's correction must
364
+ * differ from the previous one's by 1 second.
365
+ */
366
+ if (tr - prevtr < 28 * SECSPERDAY - 1
367
+ || (corr != prevcorr - 1 && corr != prevcorr + 1 ))
368
+ return EINVAL ;
369
+ sp -> lsis [leapcnt ].ls_trans = prevtr = tr ;
370
+ sp -> lsis [leapcnt ].ls_corr = prevcorr = corr ;
371
371
leapcnt ++ ;
372
372
}
373
373
}
@@ -508,7 +508,7 @@ tzloadbody(char const * name, char *canonname, struct state * sp, bool doextend,
508
508
}
509
509
510
510
/*
511
- * If type 0 is is unused in transitions, it's the type to use for early
511
+ * If type 0 is unused in transitions, it's the type to use for early
512
512
* times.
513
513
*/
514
514
for (i = 0 ; i < sp -> timecnt ; ++ i )
@@ -553,7 +553,7 @@ tzloadbody(char const * name, char *canonname, struct state * sp, bool doextend,
553
553
* given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
554
554
*/
555
555
int
556
- tzload (const char * name , char * canonname , struct state * sp , bool doextend )
556
+ tzload (const char * name , char * canonname , struct state * sp , bool doextend )
557
557
{
558
558
union local_storage * lsp = malloc (sizeof * lsp );
559
559
@@ -569,7 +569,7 @@ tzload(const char *name, char *canonname, struct state * sp, bool doextend)
569
569
}
570
570
571
571
static bool
572
- typesequiv (const struct state * sp , int a , int b )
572
+ typesequiv (const struct state * sp , int a , int b )
573
573
{
574
574
bool result ;
575
575
@@ -737,7 +737,7 @@ getoffset(const char *strp, int32 *offsetp)
737
737
* Otherwise, return a pointer to the first character not part of the rule.
738
738
*/
739
739
static const char *
740
- getrule (const char * strp , struct rule * rulep )
740
+ getrule (const char * strp , struct rule * rulep )
741
741
{
742
742
if (* strp == 'J' )
743
743
{
@@ -788,7 +788,7 @@ getrule(const char *strp, struct rule * rulep)
788
788
strp = getoffset (strp , & rulep -> r_time );
789
789
}
790
790
else
791
- rulep -> r_time = 2 * SECSPERHOUR ; /* default = 2:00:00 */
791
+ rulep -> r_time = 2 * SECSPERHOUR ; /* default = 2:00:00 */
792
792
return strp ;
793
793
}
794
794
@@ -797,7 +797,7 @@ getrule(const char *strp, struct rule * rulep)
797
797
* effect, calculate the year-relative time that rule takes effect.
798
798
*/
799
799
static int32
800
- transtime (int year , const struct rule * rulep ,
800
+ transtime (int year , const struct rule * rulep ,
801
801
int32 offset )
802
802
{
803
803
bool leapyear ;
@@ -894,7 +894,7 @@ transtime(int year, const struct rule * rulep,
894
894
* Returns true on success, false on failure.
895
895
*/
896
896
bool
897
- tzparse (const char * name , struct state * sp , bool lastditch )
897
+ tzparse (const char * name , struct state * sp , bool lastditch )
898
898
{
899
899
const char * stdname ;
900
900
const char * dstname = NULL ;
@@ -921,7 +921,7 @@ tzparse(const char *name, struct state * sp, bool lastditch)
921
921
stdlen = (sizeof sp -> chars ) - 1 ;
922
922
charcnt = stdlen + 1 ;
923
923
stdoffset = 0 ;
924
- sp -> goback = sp -> goahead = false; /* simulate failed tzload() */
924
+ sp -> goback = sp -> goahead = false; /* simulate failed tzload() */
925
925
load_ok = false;
926
926
}
927
927
else
@@ -1217,7 +1217,7 @@ tzparse(const char *name, struct state * sp, bool lastditch)
1217
1217
}
1218
1218
1219
1219
static void
1220
- gmtload (struct state * sp )
1220
+ gmtload (struct state * sp )
1221
1221
{
1222
1222
if (tzload (gmt , NULL , sp , true) != 0 )
1223
1223
tzparse (gmt , sp , true);
@@ -1231,8 +1231,8 @@ gmtload(struct state * sp)
1231
1231
* but it *is* desirable.)
1232
1232
*/
1233
1233
static struct pg_tm *
1234
- localsub (struct state const * sp , pg_time_t const * timep ,
1235
- struct pg_tm * tmp )
1234
+ localsub (struct state const * sp , pg_time_t const * timep ,
1235
+ struct pg_tm * tmp )
1236
1236
{
1237
1237
const struct ttinfo * ttisp ;
1238
1238
int i ;
@@ -1323,7 +1323,7 @@ pg_localtime(const pg_time_t *timep, const pg_tz *tz)
1323
1323
* Except we have a private "struct state" for GMT, so no sp is passed in.
1324
1324
*/
1325
1325
static struct pg_tm *
1326
- gmtsub (pg_time_t const * timep , int32 offset , struct pg_tm * tmp )
1326
+ gmtsub (pg_time_t const * timep , int32 offset , struct pg_tm * tmp )
1327
1327
{
1328
1328
struct pg_tm * result ;
1329
1329
@@ -1361,16 +1361,23 @@ pg_gmtime(const pg_time_t *timep)
1361
1361
* Return the number of leap years through the end of the given year
1362
1362
* where, to make the math easy, the answer for year zero is defined as zero.
1363
1363
*/
1364
+ static int
1365
+ leaps_thru_end_of_nonneg (int y )
1366
+ {
1367
+ return y / 4 - y / 100 + y / 400 ;
1368
+ }
1369
+
1364
1370
static int
1365
1371
leaps_thru_end_of (const int y )
1366
1372
{
1367
- return (y >= 0 ) ? (y / 4 - y / 100 + y / 400 ) :
1368
- - (leaps_thru_end_of (- (y + 1 )) + 1 );
1373
+ return (y < 0
1374
+ ? -1 - leaps_thru_end_of_nonneg (-1 - y )
1375
+ : leaps_thru_end_of_nonneg (y ));
1369
1376
}
1370
1377
1371
1378
static struct pg_tm *
1372
1379
timesub (const pg_time_t * timep , int32 offset ,
1373
- const struct state * sp , struct pg_tm * tmp )
1380
+ const struct state * sp , struct pg_tm * tmp )
1374
1381
{
1375
1382
const struct lsinfo * lp ;
1376
1383
pg_time_t tdays ;
@@ -1390,22 +1397,9 @@ timesub(const pg_time_t *timep, int32 offset,
1390
1397
lp = & sp -> lsis [i ];
1391
1398
if (* timep >= lp -> ls_trans )
1392
1399
{
1393
- if (* timep == lp -> ls_trans )
1394
- {
1395
- hit = ((i == 0 && lp -> ls_corr > 0 ) ||
1396
- lp -> ls_corr > sp -> lsis [i - 1 ].ls_corr );
1397
- if (hit )
1398
- while (i > 0 &&
1399
- sp -> lsis [i ].ls_trans ==
1400
- sp -> lsis [i - 1 ].ls_trans + 1 &&
1401
- sp -> lsis [i ].ls_corr ==
1402
- sp -> lsis [i - 1 ].ls_corr + 1 )
1403
- {
1404
- ++ hit ;
1405
- -- i ;
1406
- }
1407
- }
1408
1400
corr = lp -> ls_corr ;
1401
+ hit = (* timep == lp -> ls_trans
1402
+ && (i == 0 ? 0 : lp [-1 ].ls_corr ) < corr );
1409
1403
break ;
1410
1404
}
1411
1405
}
@@ -1529,13 +1523,13 @@ increment_overflow_time(pg_time_t *tp, int32 j)
1529
1523
{
1530
1524
/*----------
1531
1525
* This is like
1532
- * 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max )) ...',
1526
+ * 'if (! (TIME_T_MIN <= *tp + j && *tp + j <= TIME_T_MAX )) ...',
1533
1527
* except that it does the right thing even if *tp + j would overflow.
1534
1528
*----------
1535
1529
*/
1536
1530
if (!(j < 0
1537
- ? (TYPE_SIGNED (pg_time_t ) ? time_t_min - j <= * tp : -1 - j < * tp )
1538
- : * tp <= time_t_max - j ))
1531
+ ? (TYPE_SIGNED (pg_time_t ) ? TIME_T_MIN - j <= * tp : -1 - j < * tp )
1532
+ : * tp <= TIME_T_MAX - j ))
1539
1533
return true;
1540
1534
* tp += j ;
1541
1535
return false;
0 commit comments