Skip to content

Commit

Permalink
Fix GH-11281: DateTimeZone::getName() does not include seconds in offset
Browse files Browse the repository at this point in the history
If the seconds portion is non-zero, include the seconds in the output.

Closes GH-11282.
  • Loading branch information
nielsdos committed May 23, 2023
1 parent 5cad1a7 commit f9117eb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -10,6 +10,10 @@ PHP NEWS
. Fixed bug GH-11222 (foreach by-ref may jump over keys during a rehash).
(Bob)

- Date:
. Fixed bug GH-11281 (DateTimeZone::getName() does not include seconds in
offset). (nielsdos)

- Exif:
. Fixed bug GH-10834 (exif_read_data() cannot read smaller stream wrapper
chunk sizes). (nielsdos)
Expand Down
18 changes: 15 additions & 3 deletions ext/date/php_date.c
Expand Up @@ -1957,13 +1957,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);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/date/tests/bug81097.phpt
Expand Up @@ -10,5 +10,5 @@ object(DateTimeZone)#%d (%d) {
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+01:45"
string(9) "+01:45:30"
}
2 changes: 1 addition & 1 deletion ext/date/tests/bug81565.phpt
Expand Up @@ -17,4 +17,4 @@ DateTime::__set_state(array(
'timezone_type' => 1,
'timezone' => '+00:49',
))
+01:45
+01:45:30
33 changes: 33 additions & 0 deletions ext/date/tests/gh11281.phpt
@@ -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

0 comments on commit f9117eb

Please sign in to comment.