Skip to content

Commit c62b8ea

Browse files
committed
Fix edge-case behavior of pg_next_dst_boundary().
Due to rather sloppy thinking (on my part, I'm afraid) about the appropriate behavior for boundary conditions, pg_next_dst_boundary() gave undefined, platform-dependent results when the input time is exactly the last recorded DST transition time for the specified time zone, as a result of fetching values one past the end of its data arrays. Change its specification to be that it always finds the next DST boundary *after* the input time, and adjust code to match that. The sole existing caller, DetermineTimeZoneOffset, doesn't actually care about this distinction, since it always uses a probe time earlier than the instant that it does care about. So it seemed best to me to change the API to make the result=1 and result=0 cases more consistent, specifically to ensure that the "before" outputs always describe the state at the given time, rather than hacking the code to obey the previous API comment exactly. Per bug #6605 from Sergey Burladyan. Back-patch to all supported versions.
1 parent ca1e1a8 commit c62b8ea

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/timezone/localtime.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -1292,20 +1292,21 @@ increment_overflow(int *number, int delta)
12921292
}
12931293

12941294
/*
1295-
* Find the next DST transition time at or after the given time
1295+
* Find the next DST transition time after the given time
12961296
*
12971297
* *timep is the input value, the other parameters are output values.
12981298
*
12991299
* When the function result is 1, *boundary is set to the time_t
1300-
* representation of the next DST transition time at or after *timep,
1300+
* representation of the next DST transition time after *timep,
13011301
* *before_gmtoff and *before_isdst are set to the GMT offset and isdst
1302-
* state prevailing just before that boundary, and *after_gmtoff and
1303-
* *after_isdst are set to the state prevailing just after that boundary.
1302+
* state prevailing just before that boundary (in particular, the state
1303+
* prevailing at *timep), and *after_gmtoff and *after_isdst are set to
1304+
* the state prevailing just after that boundary.
13041305
*
1305-
* When the function result is 0, there is no known DST transition at or
1306+
* When the function result is 0, there is no known DST transition
13061307
* after *timep, but *before_gmtoff and *before_isdst indicate the GMT
13071308
* offset and isdst state prevailing at *timep. (This would occur in
1308-
* DST-less time zones, for example.)
1309+
* DST-less time zones, or if a zone has permanently ceased using DST.)
13091310
*
13101311
* A function result of -1 indicates failure (this case does not actually
13111312
* occur in our current implementation).
@@ -1385,16 +1386,16 @@ pg_next_dst_boundary(const pg_time_t *timep,
13851386
return result;
13861387
}
13871388

1388-
if (t > sp->ats[sp->timecnt - 1])
1389+
if (t >= sp->ats[sp->timecnt - 1])
13891390
{
1390-
/* No known transition >= t, so use last known segment's type */
1391+
/* No known transition > t, so use last known segment's type */
13911392
i = sp->types[sp->timecnt - 1];
13921393
ttisp = &sp->ttis[i];
13931394
*before_gmtoff = ttisp->tt_gmtoff;
13941395
*before_isdst = ttisp->tt_isdst;
13951396
return 0;
13961397
}
1397-
if (t <= sp->ats[0])
1398+
if (t < sp->ats[0])
13981399
{
13991400
/* For "before", use lowest-numbered standard type */
14001401
i = 0;
@@ -1415,10 +1416,10 @@ pg_next_dst_boundary(const pg_time_t *timep,
14151416
*after_isdst = ttisp->tt_isdst;
14161417
return 1;
14171418
}
1418-
/* Else search to find the containing segment */
1419+
/* Else search to find the boundary following t */
14191420
{
14201421
int lo = 1;
1421-
int hi = sp->timecnt;
1422+
int hi = sp->timecnt - 1;
14221423

14231424
while (lo < hi)
14241425
{

0 commit comments

Comments
 (0)