Skip to content

Commit caab608

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix GH-11281: DateTimeZone::getName() does not include seconds in offset
2 parents f656344 + f9117eb commit caab608

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ PHP NEWS
1313
. Fixed bug GH-11222 (foreach by-ref may jump over keys during a rehash).
1414
(Bob)
1515

16+
- Date:
17+
. Fixed bug GH-11281 (DateTimeZone::getName() does not include seconds in
18+
offset). (nielsdos)
19+
1620
- Exif:
1721
. Fixed bug GH-10834 (exif_read_data() cannot read smaller stream wrapper
1822
chunk sizes). (nielsdos)

ext/date/php_date.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,13 +2024,25 @@ static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
20242024
ZVAL_STRING(zv, tzobj->tzi.tz->name);
20252025
break;
20262026
case TIMELIB_ZONETYPE_OFFSET: {
2027-
zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
20282027
timelib_sll utc_offset = tzobj->tzi.utc_offset;
2028+
int seconds = utc_offset % 60;
2029+
size_t size;
2030+
const char *format;
2031+
if (seconds == 0) {
2032+
size = sizeof("+05:00");
2033+
format = "%c%02d:%02d";
2034+
} else {
2035+
size = sizeof("+05:00:01");
2036+
format = "%c%02d:%02d:%02d";
2037+
}
2038+
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
20292039

2030-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
2040+
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
2041+
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
20312042
utc_offset < 0 ? '-' : '+',
20322043
abs((int)(utc_offset / 3600)),
2033-
abs((int)(utc_offset % 3600) / 60));
2044+
abs((int)(utc_offset % 3600) / 60),
2045+
abs(seconds));
20342046

20352047
ZVAL_NEW_STR(zv, tmpstr);
20362048
}

ext/date/tests/bug81097.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ object(DateTimeZone)#%d (%d) {
1010
["timezone_type"]=>
1111
int(1)
1212
["timezone"]=>
13-
string(6) "+01:45"
13+
string(9) "+01:45:30"
1414
}

ext/date/tests/bug81565.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ echo "\n", (new DatetimeZone('+01:45:30'))->getName();
1717
'timezone_type' => 1,
1818
'timezone' => '+00:49',
1919
))
20-
+01:45
20+
+01:45:30

ext/date/tests/gh11281.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
GH-11281 (DateTimeZone::getName() does not include seconds in offset)
3+
--FILE--
4+
<?php
5+
$tz = new DateTimeZone('+03:00');
6+
echo $tz->getName(), "\n";
7+
$tz = new DateTimeZone('+03:00:00');
8+
echo $tz->getName(), "\n";
9+
$tz = new DateTimeZone('-03:00:00');
10+
echo $tz->getName(), "\n";
11+
$tz = new DateTimeZone('+03:00:01');
12+
echo $tz->getName(), "\n";
13+
$tz = new DateTimeZone('-03:00:01');
14+
echo $tz->getName(), "\n";
15+
$tz = new DateTimeZone('+03:00:58');
16+
echo $tz->getName(), "\n";
17+
$tz = new DateTimeZone('-03:00:58');
18+
echo $tz->getName(), "\n";
19+
$tz = new DateTimeZone('+03:00:59');
20+
echo $tz->getName(), "\n";
21+
$tz = new DateTimeZone('-03:00:59');
22+
echo $tz->getName(), "\n";
23+
?>
24+
--EXPECT--
25+
+03:00
26+
+03:00
27+
-03:00
28+
+03:00:01
29+
-03:00:01
30+
+03:00:58
31+
-03:00:58
32+
+03:00:59
33+
-03:00:59

0 commit comments

Comments
 (0)