Skip to content

Commit ee26417

Browse files
committed
Fix timezone offset with seconds losing precision
There are two issues: 1. The 'e' formatter doesn't output the seconds of the timezone even if it has seconds. 2. var_dump(), (array) cast, serialization, ... don't include the timezone second offset in the output. This means that, for example, serializing and then unserializing a date object loses the seconds of the timezone. This can be observed by comparing the output of getTimezone() for `$dt` vs the unserialized object in the provided test. Closes GH-20764.
1 parent 3cb85cc commit ee26417

File tree

4 files changed

+102
-41
lines changed

4 files changed

+102
-41
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818
- Date:
1919
. Fixed bug GH-20936 (DatePeriod::__set_state() cannot handle null start).
2020
(ndossche)
21+
. Fix timezone offset with seconds losing precision. (ndossche)
2122

2223
- DOM:
2324
. Fixed bug GH-21077 (Accessing Dom\Node::baseURI can throw TypeError).

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, timelib_t
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;
@@ -1930,6 +1941,32 @@ static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table,
19301941
return zend_std_get_properties(object);
19311942
} /* }}} */
19321943

1944+
static zend_string *date_create_tz_offset_str(timelib_sll offset)
1945+
{
1946+
int seconds = offset % 60;
1947+
size_t size;
1948+
const char *format;
1949+
1950+
if (seconds == 0) {
1951+
size = sizeof("+05:00");
1952+
format = "%c%02d:%02d";
1953+
} else {
1954+
size = sizeof("+05:00:01");
1955+
format = "%c%02d:%02d:%02d";
1956+
}
1957+
1958+
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
1959+
1960+
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
1961+
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
1962+
offset < 0 ? '-' : '+',
1963+
abs((int)(offset / 3600)),
1964+
abs((int)(offset % 3600) / 60),
1965+
abs(seconds));
1966+
1967+
return tmpstr;
1968+
}
1969+
19331970
static void date_object_to_hash(php_date_obj *dateobj, HashTable *props)
19341971
{
19351972
zval zv;
@@ -1947,17 +1984,8 @@ static void date_object_to_hash(php_date_obj *dateobj, HashTable *props)
19471984
case TIMELIB_ZONETYPE_ID:
19481985
ZVAL_STRING(&zv, dateobj->time->tz_info->name);
19491986
break;
1950-
case TIMELIB_ZONETYPE_OFFSET: {
1951-
zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
1952-
int utc_offset = dateobj->time->z;
1953-
1954-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
1955-
utc_offset < 0 ? '-' : '+',
1956-
abs(utc_offset / 3600),
1957-
abs(((utc_offset % 3600) / 60)));
1958-
1959-
ZVAL_NEW_STR(&zv, tmpstr);
1960-
}
1987+
case TIMELIB_ZONETYPE_OFFSET:
1988+
ZVAL_NEW_STR(&zv, date_create_tz_offset_str(dateobj->time->z));
19611989
break;
19621990
case TIMELIB_ZONETYPE_ABBR:
19631991
ZVAL_STRING(&zv, dateobj->time->tz_abbr);
@@ -2069,29 +2097,8 @@ static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
20692097
case TIMELIB_ZONETYPE_ID:
20702098
ZVAL_STRING(zv, tzobj->tzi.tz->name);
20712099
break;
2072-
case TIMELIB_ZONETYPE_OFFSET: {
2073-
timelib_sll utc_offset = tzobj->tzi.utc_offset;
2074-
int seconds = utc_offset % 60;
2075-
size_t size;
2076-
const char *format;
2077-
if (seconds == 0) {
2078-
size = sizeof("+05:00");
2079-
format = "%c%02d:%02d";
2080-
} else {
2081-
size = sizeof("+05:00:01");
2082-
format = "%c%02d:%02d:%02d";
2083-
}
2084-
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
2085-
2086-
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
2087-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
2088-
utc_offset < 0 ? '-' : '+',
2089-
abs((int)(utc_offset / 3600)),
2090-
abs((int)(utc_offset % 3600) / 60),
2091-
abs(seconds));
2092-
2093-
ZVAL_NEW_STR(zv, tmpstr);
2094-
}
2100+
case TIMELIB_ZONETYPE_OFFSET:
2101+
ZVAL_NEW_STR(zv, date_create_tz_offset_str(tzobj->tzi.utc_offset));
20952102
break;
20962103
case TIMELIB_ZONETYPE_ABBR:
20972104
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)