Skip to content

Commit 001e7db

Browse files
committedJul 28, 2022
Fixed bug #80047 (DatePeriod doesn't warn with custom DateTimeImmutable)
1 parent 85f3a96 commit 001e7db

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
 

‎NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2022, PHP 8.0.23
44

5+
- Date:
6+
. Fixed bug #80047 (DatePeriod doesn't warn with custom DateTimeImmutable).
7+
(Derick)
8+
59
- DBA:
610
. Fixed LMDB driver memory leak on DB creation failure (Girgias)
711
. Fixed bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults).

‎ext/date/php_date.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,17 @@ static int date_period_it_has_more(zend_object_iterator *iter)
14751475
}
14761476
/* }}} */
14771477

1478+
static zend_class_entry *get_base_date_class(zend_class_entry *start_ce)
1479+
{
1480+
zend_class_entry *tmp = start_ce;
1481+
1482+
while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) {
1483+
tmp = tmp->parent;
1484+
}
1485+
1486+
return tmp;
1487+
}
1488+
14781489
/* {{{ date_period_it_current_data */
14791490
static zval *date_period_it_current_data(zend_object_iterator *iter)
14801491
{
@@ -1484,7 +1495,7 @@ static zval *date_period_it_current_data(zend_object_iterator *iter)
14841495
php_date_obj *newdateobj;
14851496

14861497
/* Create new object */
1487-
php_date_instantiate(object->start_ce, &iterator->current);
1498+
php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
14881499
newdateobj = Z_PHPDATE_P(&iterator->current);
14891500
newdateobj->time = timelib_time_ctor();
14901501
*newdateobj->time = *it_time;

‎ext/date/tests/bug80047.phpt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #80047: DatePeriod doesn't support custom DateTimeImmutable
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
class CustomDateTime extends DateTime {}
8+
class CustomDateTimeImmutable extends DateTimeImmutable {}
9+
10+
$dt = new DateTime('2022-06-24');
11+
$dti = new DateTimeImmutable('2022-06-24');
12+
$cdt = new CustomDateTime('2022-06-25');
13+
$cdti = new CustomDateTimeImmutable('2022-06-25');
14+
$i = new DateInterval('P1D');
15+
16+
$tests = [
17+
[ $dt, $i, $cdt ],
18+
[ $cdt, $i, $dt ],
19+
[ $cdt, $i, $cdt ],
20+
[ $dti, $i, $cdti ],
21+
[ $cdti, $i, $dti ],
22+
[ $cdti, $i, $cdti ],
23+
[ $cdt, $i, $cdti ],
24+
];
25+
26+
foreach ($tests as $test) {
27+
$dp = new DatePeriod(...$test);
28+
foreach ($dp as $date) {}
29+
echo get_class($date), "\n";
30+
}
31+
?>
32+
--EXPECT--
33+
DateTime
34+
DateTime
35+
DateTime
36+
DateTimeImmutable
37+
DateTimeImmutable
38+
DateTimeImmutable
39+
DateTimeImmutable

0 commit comments

Comments
 (0)