Skip to content

Commit 46e8cb9

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix timezone offset with seconds losing precision
2 parents dcf5cd9 + e76044a commit 46e8cb9

3 files changed

Lines changed: 101 additions & 41 deletions

File tree

ext/date/php_date.c

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,24 @@ static zend_string *date_format(const char *format, size_t format_len, const tim
795795
case TIMELIB_ZONETYPE_ABBR:
796796
length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
797797
break;
798-
case TIMELIB_ZONETYPE_OFFSET:
799-
length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
800-
((offset->offset < 0) ? '-' : '+'),
801-
abs(offset->offset / 3600),
802-
abs((offset->offset % 3600) / 60)
803-
);
798+
case TIMELIB_ZONETYPE_OFFSET: {
799+
int seconds = offset->offset % 60;
800+
if (seconds == 0) {
801+
length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
802+
((offset->offset < 0) ? '-' : '+'),
803+
abs(offset->offset / 3600),
804+
abs((offset->offset % 3600) / 60)
805+
);
806+
} else {
807+
length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d:%02d",
808+
((offset->offset < 0) ? '-' : '+'),
809+
abs(offset->offset / 3600),
810+
abs((offset->offset % 3600) / 60),
811+
abs(seconds)
812+
);
813+
}
804814
break;
815+
}
805816
}
806817
}
807818
break;
@@ -1892,6 +1903,32 @@ static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table,
18921903
return zend_std_get_properties(object);
18931904
} /* }}} */
18941905

1906+
static zend_string *date_create_tz_offset_str(timelib_sll offset)
1907+
{
1908+
int seconds = offset % 60;
1909+
size_t size;
1910+
const char *format;
1911+
1912+
if (seconds == 0) {
1913+
size = sizeof("+05:00");
1914+
format = "%c%02d:%02d";
1915+
} else {
1916+
size = sizeof("+05:00:01");
1917+
format = "%c%02d:%02d:%02d";
1918+
}
1919+
1920+
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
1921+
1922+
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
1923+
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
1924+
offset < 0 ? '-' : '+',
1925+
abs((int)(offset / 3600)),
1926+
abs((int)(offset % 3600) / 60),
1927+
abs(seconds));
1928+
1929+
return tmpstr;
1930+
}
1931+
18951932
static void date_object_to_hash(php_date_obj *dateobj, HashTable *props)
18961933
{
18971934
zval zv;
@@ -1909,17 +1946,8 @@ static void date_object_to_hash(php_date_obj *dateobj, HashTable *props)
19091946
case TIMELIB_ZONETYPE_ID:
19101947
ZVAL_STRING(&zv, dateobj->time->tz_info->name);
19111948
break;
1912-
case TIMELIB_ZONETYPE_OFFSET: {
1913-
zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
1914-
int utc_offset = dateobj->time->z;
1915-
1916-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
1917-
utc_offset < 0 ? '-' : '+',
1918-
abs(utc_offset / 3600),
1919-
abs(((utc_offset % 3600) / 60)));
1920-
1921-
ZVAL_NEW_STR(&zv, tmpstr);
1922-
}
1949+
case TIMELIB_ZONETYPE_OFFSET:
1950+
ZVAL_NEW_STR(&zv, date_create_tz_offset_str(dateobj->time->z));
19231951
break;
19241952
case TIMELIB_ZONETYPE_ABBR:
19251953
ZVAL_STRING(&zv, dateobj->time->tz_abbr);
@@ -2031,29 +2059,8 @@ static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
20312059
case TIMELIB_ZONETYPE_ID:
20322060
ZVAL_STRING(zv, tzobj->tzi.tz->name);
20332061
break;
2034-
case TIMELIB_ZONETYPE_OFFSET: {
2035-
timelib_sll utc_offset = tzobj->tzi.utc_offset;
2036-
int seconds = utc_offset % 60;
2037-
size_t size;
2038-
const char *format;
2039-
if (seconds == 0) {
2040-
size = sizeof("+05:00");
2041-
format = "%c%02d:%02d";
2042-
} else {
2043-
size = sizeof("+05:00:01");
2044-
format = "%c%02d:%02d:%02d";
2045-
}
2046-
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
2047-
2048-
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
2049-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
2050-
utc_offset < 0 ? '-' : '+',
2051-
abs((int)(utc_offset / 3600)),
2052-
abs((int)(utc_offset % 3600) / 60),
2053-
abs(seconds));
2054-
2055-
ZVAL_NEW_STR(zv, tmpstr);
2056-
}
2062+
case TIMELIB_ZONETYPE_OFFSET:
2063+
ZVAL_NEW_STR(zv, date_create_tz_offset_str(tzobj->tzi.utc_offset));
20572064
break;
20582065
case TIMELIB_ZONETYPE_ABBR:
20592066
ZVAL_STRING(zv, tzobj->tzi.z.abbr);

ext/date/tests/bug81565.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ echo "\n", (new DatetimeZone('+01:45:30'))->getName();
1515
\DateTime::__set_state(array(
1616
'date' => '0021-08-21 00:00:00.000000',
1717
'timezone_type' => 1,
18-
'timezone' => '+00:49',
18+
'timezone' => '+00:49:56',
1919
))
2020
+01:45:30

ext/date/tests/gh20764.phpt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
GH-20764 (Timezone offset with seconds loses precision)
3+
--FILE--
4+
<?php
5+
6+
$timezones = [
7+
'+03:00:30',
8+
'-03:00:30',
9+
];
10+
11+
foreach ($timezones as $timezone) {
12+
echo "--- Testing timezone $timezone ---\n";
13+
$tz = new DateTimeZone($timezone);
14+
$dt = new DateTimeImmutable('2025-04-01', $tz);
15+
var_dump($dt->format('e'));
16+
var_dump($dt);
17+
var_dump(unserialize(serialize($dt))->getTimezone());
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
--- Testing timezone +03:00:30 ---
23+
string(9) "+03:00:30"
24+
object(DateTimeImmutable)#%d (3) {
25+
["date"]=>
26+
string(26) "2025-04-01 00:00:00.000000"
27+
["timezone_type"]=>
28+
int(1)
29+
["timezone"]=>
30+
string(9) "+03:00:30"
31+
}
32+
object(DateTimeZone)#%d (2) {
33+
["timezone_type"]=>
34+
int(1)
35+
["timezone"]=>
36+
string(9) "+03:00:30"
37+
}
38+
--- Testing timezone -03:00:30 ---
39+
string(9) "-03:00:30"
40+
object(DateTimeImmutable)#%d (3) {
41+
["date"]=>
42+
string(26) "2025-04-01 00:00:00.000000"
43+
["timezone_type"]=>
44+
int(1)
45+
["timezone"]=>
46+
string(9) "-03:00:30"
47+
}
48+
object(DateTimeZone)#%d (2) {
49+
["timezone_type"]=>
50+
int(1)
51+
["timezone"]=>
52+
string(9) "-03:00:30"
53+
}

0 commit comments

Comments
 (0)