Skip to content

Commit f9117eb

Browse files
committed
Fix GH-11281: DateTimeZone::getName() does not include seconds in offset
If the seconds portion is non-zero, include the seconds in the output. Closes GH-11282.
1 parent 5cad1a7 commit f9117eb

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
@@ -10,6 +10,10 @@ PHP NEWS
1010
. Fixed bug GH-11222 (foreach by-ref may jump over keys during a rehash).
1111
(Bob)
1212

13+
- Date:
14+
. Fixed bug GH-11281 (DateTimeZone::getName() does not include seconds in
15+
offset). (nielsdos)
16+
1317
- Exif:
1418
. Fixed bug GH-10834 (exif_read_data() cannot read smaller stream wrapper
1519
chunk sizes). (nielsdos)

ext/date/php_date.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,13 +1957,25 @@ static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
19571957
ZVAL_STRING(zv, tzobj->tzi.tz->name);
19581958
break;
19591959
case TIMELIB_ZONETYPE_OFFSET: {
1960-
zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
19611960
timelib_sll utc_offset = tzobj->tzi.utc_offset;
1961+
int seconds = utc_offset % 60;
1962+
size_t size;
1963+
const char *format;
1964+
if (seconds == 0) {
1965+
size = sizeof("+05:00");
1966+
format = "%c%02d:%02d";
1967+
} else {
1968+
size = sizeof("+05:00:01");
1969+
format = "%c%02d:%02d:%02d";
1970+
}
1971+
zend_string *tmpstr = zend_string_alloc(size - 1, 0);
19621972

1963-
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
1973+
/* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
1974+
ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
19641975
utc_offset < 0 ? '-' : '+',
19651976
abs((int)(utc_offset / 3600)),
1966-
abs((int)(utc_offset % 3600) / 60));
1977+
abs((int)(utc_offset % 3600) / 60),
1978+
abs(seconds));
19671979

19681980
ZVAL_NEW_STR(zv, tmpstr);
19691981
}

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 @@ DateTime::__set_state(array(
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)