mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
Merge branch 'PHP-8.2'
* PHP-8.2: Fix GH-11281: DateTimeZone::getName() does not include seconds in offset
This commit is contained in:
@@ -2055,13 +2055,25 @@ static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
|
||||
ZVAL_STRING(zv, tzobj->tzi.tz->name);
|
||||
break;
|
||||
case TIMELIB_ZONETYPE_OFFSET: {
|
||||
zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
|
||||
timelib_sll utc_offset = tzobj->tzi.utc_offset;
|
||||
int seconds = utc_offset % 60;
|
||||
size_t size;
|
||||
const char *format;
|
||||
if (seconds == 0) {
|
||||
size = sizeof("+05:00");
|
||||
format = "%c%02d:%02d";
|
||||
} else {
|
||||
size = sizeof("+05:00:01");
|
||||
format = "%c%02d:%02d:%02d";
|
||||
}
|
||||
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
|
||||
|
||||
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
|
||||
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
|
||||
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
|
||||
utc_offset < 0 ? '-' : '+',
|
||||
abs((int)(utc_offset / 3600)),
|
||||
abs((int)(utc_offset % 3600) / 60));
|
||||
abs((int)(utc_offset % 3600) / 60),
|
||||
abs(seconds));
|
||||
|
||||
ZVAL_NEW_STR(zv, tmpstr);
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ object(DateTimeZone)#%d (%d) {
|
||||
["timezone_type"]=>
|
||||
int(1)
|
||||
["timezone"]=>
|
||||
string(6) "+01:45"
|
||||
string(9) "+01:45:30"
|
||||
}
|
||||
|
||||
@@ -17,4 +17,4 @@ echo "\n", (new DatetimeZone('+01:45:30'))->getName();
|
||||
'timezone_type' => 1,
|
||||
'timezone' => '+00:49',
|
||||
))
|
||||
+01:45
|
||||
+01:45:30
|
||||
|
||||
33
ext/date/tests/gh11281.phpt
Normal file
33
ext/date/tests/gh11281.phpt
Normal file
@@ -0,0 +1,33 @@
|
||||
--TEST--
|
||||
GH-11281 (DateTimeZone::getName() does not include seconds in offset)
|
||||
--FILE--
|
||||
<?php
|
||||
$tz = new DateTimeZone('+03:00');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('+03:00:00');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('-03:00:00');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('+03:00:01');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('-03:00:01');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('+03:00:58');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('-03:00:58');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('+03:00:59');
|
||||
echo $tz->getName(), "\n";
|
||||
$tz = new DateTimeZone('-03:00:59');
|
||||
echo $tz->getName(), "\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
+03:00
|
||||
+03:00
|
||||
-03:00
|
||||
+03:00:01
|
||||
-03:00:01
|
||||
+03:00:58
|
||||
-03:00:58
|
||||
+03:00:59
|
||||
-03:00:59
|
||||
Reference in New Issue
Block a user