Skip to content

Commit 534127d

Browse files
committed
Merge branch 'PHP-8.1'
2 parents 1a9e689 + b0d67aa commit 534127d

File tree

3 files changed

+51
-56
lines changed

3 files changed

+51
-56
lines changed

ext/date/php_date.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,17 @@ static int date_period_it_has_more(zend_object_iterator *iter)
15361536
}
15371537
/* }}} */
15381538

1539+
static zend_class_entry *get_base_date_class(zend_class_entry *start_ce)
1540+
{
1541+
zend_class_entry *tmp = start_ce;
1542+
1543+
while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) {
1544+
tmp = tmp->parent;
1545+
}
1546+
1547+
return tmp;
1548+
}
1549+
15391550
/* {{{ date_period_it_current_data */
15401551
static zval *date_period_it_current_data(zend_object_iterator *iter)
15411552
{
@@ -1545,7 +1556,7 @@ static zval *date_period_it_current_data(zend_object_iterator *iter)
15451556
php_date_obj *newdateobj;
15461557

15471558
/* Create new object */
1548-
php_date_instantiate(object->start_ce, &iterator->current);
1559+
php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
15491560
newdateobj = Z_PHPDATE_P(&iterator->current);
15501561
newdateobj->time = timelib_time_ctor();
15511562
*newdateobj->time = *it_time;
@@ -4568,20 +4579,6 @@ PHP_METHOD(DatePeriod, __construct)
45684579
}
45694580
dpobj->start_ce = date_ce_date;
45704581
} else {
4571-
/* Sanity checks */
4572-
if (start && Z_OBJCE_P(start) != date_ce_date && Z_OBJCE_P(start) != date_ce_immutable) {
4573-
zend_string *func = get_active_function_or_method_name();
4574-
zend_throw_error(zend_ce_exception, "%s(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class %s provided", ZSTR_VAL(func), ZSTR_VAL(Z_OBJCE_P(start)->name));
4575-
zend_string_release(func);
4576-
RETURN_THROWS();
4577-
}
4578-
if (end && Z_OBJCE_P(end) != date_ce_date && Z_OBJCE_P(end) != date_ce_immutable) {
4579-
zend_string *func = get_active_function_or_method_name();
4580-
zend_throw_error(zend_ce_exception, "%s(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class %s provided", ZSTR_VAL(func), ZSTR_VAL(Z_OBJCE_P(end)->name));
4581-
zend_string_release(func);
4582-
RETURN_THROWS();
4583-
}
4584-
45854582
/* init */
45864583
php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
45874584

ext/date/tests/bug80042.phpt

Lines changed: 0 additions & 41 deletions
This file was deleted.

ext/date/tests/bug80047.phpt

Lines changed: 39 additions & 0 deletions
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)