Fixed bug #52015 (Allow including end date in DatePeriod iterations)

This commit is contained in:
Derick Rethans 2022-05-13 14:20:05 +01:00
parent 852e6b957b
commit c0c801d238
3 changed files with 29 additions and 2 deletions

View file

@ -1430,6 +1430,7 @@ PHP_FUNCTION(getdate)
#define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
#define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
#define PHP_DATE_PERIOD_INCLUDE_END_DATE 0x0002
/* define an overloaded iterator structure */
@ -1470,7 +1471,11 @@ static int date_period_it_has_more(zend_object_iterator *iter)
php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
if (object->end) {
return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
if (object->include_end_date) {
return object->current->sse <= object->end->sse ? SUCCESS : FAILURE;
} else {
return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
}
} else {
return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
}
@ -1734,6 +1739,7 @@ static void date_register_classes(void) /* {{{ */
zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value);
REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
REGISTER_PERIOD_CLASS_CONST_STRING("INCLUDE_END_DATE", PHP_DATE_PERIOD_INCLUDE_END_DATE);
} /* }}} */
static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
@ -4567,9 +4573,10 @@ PHP_METHOD(DatePeriod, __construct)
/* options */
dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
dpobj->include_end_date = options & PHP_DATE_PERIOD_INCLUDE_END_DATE;
/* recurrrences */
dpobj->recurrences = recurrences + dpobj->include_start_date;
dpobj->recurrences = recurrences + dpobj->include_start_date + dpobj->include_end_date;
dpobj->initialized = 1;
}

View file

@ -93,6 +93,7 @@ struct _php_period_obj {
int recurrences;
bool initialized;
bool include_start_date;
int include_end_date;
zend_object std;
};

View file

@ -0,0 +1,19 @@
--TEST--
DatePeriod::INCLUDE_END_DATE
--FILE--
<?php
date_default_timezone_set('UTC');
$start = new DateTime('2010-06-07');
$end = new DateTime('2010-06-10');
$interval = new DateInterval('P1D');
foreach (new DatePeriod($start, $interval, $end, DatePeriod::INCLUDE_END_DATE) as $day) {
echo $day->format('Y-m-d') . "\n";
}
?>
--EXPECT--
2010-06-07
2010-06-08
2010-06-09
2010-06-10