Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 505a30c

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

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

‎ext/date/php_date.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,13 +2055,25 @@ static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
20552055
ZVAL_STRING(zv, tzobj->tzi.tz->name);
20562056
break;
20572057
case TIMELIB_ZONETYPE_OFFSET: {
2058-
zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
20592058
timelib_sll utc_offset = tzobj->tzi.utc_offset;
2059+
int seconds = utc_offset % 60;
2060+
size_t size;
2061+
const char *format;
2062+
if (seconds == 0) {
2063+
size = sizeof("+05:00");
2064+
format = "%c%02d:%02d";
2065+
} else {
2066+
size = sizeof("+05:00:01");
2067+
format = "%c%02d:%02d:%02d";
2068+
}
2069+
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
20602070

2061-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
2071+
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
2072+
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
20622073
utc_offset < 0 ? '-' : '+',
20632074
abs((int)(utc_offset / 3600)),
2064-
abs((int)(utc_offset % 3600) / 60));
2075+
abs((int)(utc_offset % 3600) / 60),
2076+
abs(seconds));
20652077

20662078
ZVAL_NEW_STR(zv, tmpstr);
20672079
}

‎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)

AltStyle によって変換されたページ (->オリジナル) /