Skip to content

Commit 93798d3

Browse files
committed
Resolve repeated local times consistently
Fixes #24735.
1 parent 268a2e5 commit 93798d3

4 files changed

Lines changed: 139 additions & 24 deletions

File tree

‎ext/date/lib/timelib.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ void timelib_strtointerval(const char *s, size_t len,
641641
* calculate the right timestamp.
642642
*/
643643
void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi);
644+
void timelib_update_ts_first(timelib_time* time, timelib_tzinfo* tzi);
644645

645646
/**
646647
* Returns the number of days from the y/m/d fields of 'time' since the Unix

‎ext/date/lib/tm2unixtime.c‎

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,50 @@ static void do_adjust_special_early(timelib_time* time)
338338
timelib_do_normalize(time);
339339
}
340340

341-
static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
341+
static bool timelib_adjust_timezone_for_overlap(timelib_time *tz, timelib_tzinfo *tzi, int32_t preferred_offset)
342+
{
343+
timelib_sll earlier_sse = INT64_MAX;
344+
timelib_sll second_earliest_sse = INT64_MAX;
345+
timelib_sll matching_offset_sse = INT64_MAX;
346+
uint64_t type_count = tzi->bit64.typecnt;
347+
348+
for (uint64_t i = 0; i < type_count; i++) {
349+
int32_t offset = tzi->type[i].offset;
350+
int32_t actual_offset;
351+
352+
if ((offset > 0 && tz->sse < INT64_MIN + offset) || (offset < 0 && tz->sse > INT64_MAX + offset)) {
353+
continue;
354+
}
355+
356+
timelib_sll candidate_sse = tz->sse - offset;
357+
if (!timelib_get_time_zone_offset_info(candidate_sse, tzi, &actual_offset, NULL, NULL) || actual_offset != offset) {
358+
continue;
359+
}
360+
361+
if (candidate_sse < earlier_sse) {
362+
second_earliest_sse = earlier_sse;
363+
earlier_sse = candidate_sse;
364+
} else if (candidate_sse > earlier_sse && candidate_sse < second_earliest_sse) {
365+
second_earliest_sse = candidate_sse;
366+
}
367+
368+
if (offset == preferred_offset && candidate_sse < matching_offset_sse) {
369+
matching_offset_sse = candidate_sse;
370+
}
371+
}
372+
373+
if (second_earliest_sse == INT64_MAX) {
374+
return false;
375+
}
376+
377+
/* Keep the previous offset if it is valid for the new local time. */
378+
tz->sse = matching_offset_sse != INT64_MAX ? matching_offset_sse : earlier_sse;
379+
tz->is_localtime = 1;
380+
timelib_set_timezone(tz, tzi);
381+
return true;
382+
}
383+
384+
static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi, bool resolve_overlap, int32_t preferred_offset)
342385
{
343386
switch (tz->zone_type) {
344387
case TIMELIB_ZONETYPE_OFFSET:
@@ -373,33 +416,31 @@ static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
373416
if (!tzi) {
374417
return;
375418
}
419+
if (resolve_overlap && timelib_adjust_timezone_for_overlap(tz, tzi, preferred_offset)) {
420+
return;
421+
}
376422

377423
timelib_get_time_zone_offset_info(tz->sse, tzi, &current_offset, &current_transition_time, &current_is_dst);
378424
timelib_get_time_zone_offset_info(tz->sse - current_offset, tzi, &after_offset, &after_transition_time, NULL);
379425
actual_offset = after_offset;
380426
actual_transition_time = after_transition_time;
381427
if (current_offset == after_offset && tz->have_zone) {
382-
/* Make sure we're not missing a DST change because we don't know the actual offset yet */
383428
if (current_offset >= 0 && tz->dst && !current_is_dst) {
384-
/* Timezone or its DST at or east of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) before the actual local time */
385-
int32_t earlier_offset;
386-
timelib_sll earlier_transition_time;
387-
timelib_get_time_zone_offset_info(tz->sse - current_offset - 7200, tzi, &earlier_offset, &earlier_transition_time, NULL);
388-
if ((earlier_offset != after_offset) && (tz->sse - earlier_offset < after_transition_time)) {
389-
/* Looking behind a bit clarified the actual offset to use */
390-
actual_offset = earlier_offset;
391-
actual_transition_time = earlier_transition_time;
392-
}
429+
int32_t earlier_offset;
430+
timelib_sll earlier_transition_time;
431+
timelib_get_time_zone_offset_info(tz->sse - current_offset - 7200, tzi, &earlier_offset, &earlier_transition_time, NULL);
432+
if ((earlier_offset != after_offset) && (tz->sse - earlier_offset < after_transition_time)) {
433+
actual_offset = earlier_offset;
434+
actual_transition_time = earlier_transition_time;
435+
}
393436
} else if (current_offset <= 0 && current_is_dst && !tz->dst) {
394-
/* Timezone west of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) after the actual local time */
395-
int32_t later_offset;
396-
timelib_sll later_transition_time;
397-
timelib_get_time_zone_offset_info(tz->sse - current_offset + 7200, tzi, &later_offset, &later_transition_time, NULL);
398-
if ((later_offset != after_offset) && (tz->sse - later_offset >= later_transition_time)) {
399-
/* Looking ahead a bit clarified the actual offset to use */
400-
actual_offset = later_offset;
401-
actual_transition_time = later_transition_time;
402-
}
437+
int32_t later_offset;
438+
timelib_sll later_transition_time;
439+
timelib_get_time_zone_offset_info(tz->sse - current_offset + 7200, tzi, &later_offset, &later_transition_time, NULL);
440+
if ((later_offset != after_offset) && (tz->sse - later_offset >= later_transition_time)) {
441+
actual_offset = later_offset;
442+
actual_transition_time = later_transition_time;
443+
}
403444
}
404445
}
405446

@@ -439,8 +480,13 @@ timelib_sll timelib_epoch_days_from_time(timelib_time *time)
439480
return era * DAYS_PER_ERA + day_of_era - HINNANT_EPOCH_SHIFT;
440481
}
441482

442-
void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
483+
static void timelib_update_ts_internal(timelib_time* time, timelib_tzinfo* tzi, bool resolve_overlap)
443484
{
485+
int32_t preferred_offset = time->z;
486+
if (time->sse_uptodate && time->zone_type == TIMELIB_ZONETYPE_ID && time->tz_info) {
487+
timelib_get_time_zone_offset_info(time->sse, time->tz_info, &preferred_offset, NULL, NULL);
488+
}
489+
444490
do_adjust_special_early(time);
445491
do_adjust_relative(time);
446492
do_adjust_special(time);
@@ -456,12 +502,22 @@ void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
456502
time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
457503

458504
// This modifies time->sse, if needed
459-
do_adjust_timezone(time, tzi);
505+
do_adjust_timezone(time, tzi, resolve_overlap, preferred_offset);
460506

461507
time->sse_uptodate = 1;
462508
time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = time->relative.first_last_day_of = 0;
463509
}
464510

511+
void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
512+
{
513+
timelib_update_ts_internal(time, tzi, false);
514+
}
515+
516+
void timelib_update_ts_first(timelib_time* time, timelib_tzinfo* tzi)
517+
{
518+
timelib_update_ts_internal(time, tzi, true);
519+
}
520+
465521
#if 0
466522
int main(void)
467523
{

‎ext/date/php_date.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,6 +3290,9 @@ static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{
32903290

32913291
memcpy(&dateobj->time->relative, &tmp_time->relative, sizeof(timelib_rel_time));
32923292
dateobj->time->have_relative = tmp_time->have_relative;
3293+
if (dateobj->time->sse_uptodate && dateobj->time->zone_type == TIMELIB_ZONETYPE_ID && dateobj->time->tz_info) {
3294+
timelib_get_time_zone_offset_info(dateobj->time->sse, dateobj->time->tz_info, &dateobj->time->z, NULL, NULL);
3295+
}
32933296
dateobj->time->sse_uptodate = 0;
32943297

32953298
if (tmp_time->y != TIMELIB_UNSET) {
@@ -3333,7 +3336,7 @@ static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{
33333336

33343337
timelib_time_dtor(tmp_time);
33353338

3336-
timelib_update_ts(dateobj->time, NULL);
3339+
timelib_update_ts_first(dateobj->time, NULL);
33373340
timelib_update_from_sse(dateobj->time);
33383341
dateobj->time->have_relative = 0;
33393342
memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
@@ -3692,7 +3695,7 @@ static void php_date_time_set(zval *object, zend_long h, zend_long i, zend_long
36923695
dateobj->time->i = i;
36933696
dateobj->time->s = s;
36943697
dateobj->time->us = ms;
3695-
timelib_update_ts(dateobj->time, NULL);
3698+
timelib_update_ts_first(dateobj->time, NULL);
36963699
timelib_update_from_sse(dateobj->time);
36973700
} /* }}} */
36983701

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
DateTime::setTime() preserves the offset in an overlapping local time
3+
--FILE--
4+
<?php
5+
6+
foreach ([
7+
['Europe/Dublin', '2026-10-25 00:10:30 UTC', 1, 10],
8+
['Europe/London', '2026-10-25 00:10:30 UTC', 1, 10],
9+
['Europe/Amsterdam', '2026-10-25 00:10:30 UTC', 2, 10],
10+
['America/New_York', '2026-11-01 05:10:30 UTC', 1, 10],
11+
['Australia/Lord_Howe', '2026-04-04 14:45:30 UTC', 1, 45],
12+
] as [$timezone, $instant, $hour, $minute]) {
13+
$date = new DateTimeImmutable($instant);
14+
$date = $date->setTimezone(new DateTimeZone($timezone));
15+
$result = $date->setTime($hour, $minute, 59);
16+
printf("%s %s %+d\n", $timezone, $result->format('H:i:s T'), $result->getTimestamp() - $date->getTimestamp());
17+
}
18+
19+
$date = new DateTime('2026-10-25 01:10:30 UTC');
20+
$date->setTimezone(new DateTimeZone('Europe/Dublin'));
21+
$date->setTime(1, 10, 59);
22+
printf("%s %+d\n", $date->format('H:i:s T'), $date->getTimestamp() - strtotime('2026-10-25 01:10:30 UTC'));
23+
24+
$date = new DateTimeImmutable('2026-10-25 01:10:30 UTC');
25+
$date = $date->setTimezone(new DateTimeZone('Europe/Dublin'));
26+
$result = $date->setTime(1, 10, 59);
27+
printf("%s %+d\n", $result->format('H:i:s T'), $result->getTimestamp() - $date->getTimestamp());
28+
29+
$date = new DateTimeImmutable('2026-10-25 01:10:30 UTC');
30+
$date = $date->setTimezone(new DateTimeZone('Europe/London'));
31+
$result = $date->setTime(1, 10, 59);
32+
printf("%s %+d\n", $result->format('H:i:s T'), $result->getTimestamp() - $date->getTimestamp());
33+
34+
$date = new DateTimeImmutable('2026-10-25 01:10:30 UTC');
35+
$date = $date->setTimezone(new DateTimeZone('Europe/London'));
36+
$result = $date->modify('+29 seconds');
37+
printf("%s %+d\n", $result->format('H:i:s T'), $result->getTimestamp() - $date->getTimestamp());
38+
39+
$date = new DateTimeImmutable('2026-10-25 00:10:30 UTC');
40+
$date = $date->setTimezone(new DateTimeZone('Europe/Dublin'));
41+
$result = $date->modify('+29 seconds');
42+
printf("%s %+d\n", $result->format('H:i:s T'), $result->getTimestamp() - $date->getTimestamp());
43+
44+
?>
45+
--EXPECT--
46+
Europe/Dublin 01:10:59 IST +29
47+
Europe/London 01:10:59 BST +29
48+
Europe/Amsterdam 02:10:59 CEST +29
49+
America/New_York 01:10:59 EDT +29
50+
Australia/Lord_Howe 01:45:59 +11 +29
51+
01:10:59 GMT +29
52+
01:10:59 GMT +29
53+
01:10:59 GMT +29
54+
01:10:59 GMT +29
55+
01:10:59 IST +29

0 commit comments

Comments
 (0)