Skip to content

Fixed bug #52015: Allow including end date in DatePeriod iterations #8550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) /* {{{ */
Expand Down Expand Up @@ -2143,6 +2149,7 @@ static zend_object *date_object_clone_period(zend_object *this_ptr) /* {{{ */
new_obj->initialized = old_obj->initialized;
new_obj->recurrences = old_obj->recurrences;
new_obj->include_start_date = old_obj->include_start_date;
new_obj->include_end_date = old_obj->include_end_date;
new_obj->start_ce = old_obj->start_ce;

if (old_obj->start) {
Expand Down Expand Up @@ -4567,9 +4574,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;
}
Expand Down Expand Up @@ -4653,11 +4661,11 @@ PHP_METHOD(DatePeriod, getRecurrences)

dpobj = Z_PHPPERIOD_P(ZEND_THIS);

if (0 == dpobj->recurrences - dpobj->include_start_date) {
if (0 == dpobj->recurrences - dpobj->include_start_date - dpobj->include_end_date) {
return;
}

RETURN_LONG(dpobj->recurrences - dpobj->include_start_date);
RETURN_LONG(dpobj->recurrences - dpobj->include_start_date - dpobj->include_end_date);
}
/* }}} */

Expand Down Expand Up @@ -5082,6 +5090,9 @@ static void date_period_object_to_hash(php_period_obj *period_obj, HashTable *pr

ZVAL_BOOL(&zv, period_obj->include_start_date);
zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);

ZVAL_BOOL(&zv, period_obj->include_end_date);
zend_hash_str_update(props, "include_end_date", sizeof("include_end_date")-1, &zv);
}

static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
Expand Down Expand Up @@ -5175,6 +5186,14 @@ static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, Has
return 0;
}

ht_entry = zend_hash_str_find(myht, "include_end_date", sizeof("include_end_date")-1);
if (ht_entry &&
(Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
period_obj->include_end_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
} else {
return 0;
}

period_obj->initialized = 1;

return 1;
Expand Down Expand Up @@ -5267,6 +5286,7 @@ static bool date_period_is_magic_property(zend_string *name)
{
if (zend_string_equals_literal(name, "recurrences")
|| zend_string_equals_literal(name, "include_start_date")
|| zend_string_equals_literal(name, "include_end_date")
|| zend_string_equals_literal(name, "start")
|| zend_string_equals_literal(name, "current")
|| zend_string_equals_literal(name, "end")
Expand Down
1 change: 1 addition & 0 deletions ext/date/php_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct _php_period_obj {
int recurrences;
bool initialized;
bool include_start_date;
bool include_end_date;
zend_object std;
};

Expand Down
26 changes: 16 additions & 10 deletions ext/date/tests/DatePeriod_serialize-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var_dump($d->__serialize());
?>
--EXPECTF--
Original object:
object(DatePeriod)#1 (6) {
object(DatePeriod)#%d (%d) {
["start"]=>
object(DateTime)#2 (3) {
object(DateTime)#%d (%d) {
["date"]=>
string(26) "2012-07-01 00:00:00.000000"
["timezone_type"]=>
Expand All @@ -37,7 +37,7 @@ object(DatePeriod)#1 (6) {
["end"]=>
NULL
["interval"]=>
object(DateInterval)#3 (10) {
object(DateInterval)#%d (%d) {
["y"]=>
int(0)
["m"]=>
Expand All @@ -63,17 +63,19 @@ object(DatePeriod)#1 (6) {
int(5)
["include_start_date"]=>
bool(true)
["include_end_date"]=>
bool(false)
}


Serialised object:
string(411) "O:10:"DatePeriod":6:{s:5:"start";O:8:"DateTime":3:{s:4:"date";s:26:"2012-07-01 00:00:00.000000";s:13:"timezone_type";i:1;s:8:"timezone";s:6:"+00:00";}s:7:"current";N;s:3:"end";N;s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:7;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:5;s:18:"include_start_date";b:1;}"
string(%d) "O:10:"DatePeriod":7:{s:5:"start";O:8:"DateTime":3:{s:4:"date";s:26:"2012-07-01 00:00:00.000000";s:13:"timezone_type";i:1;s:8:"timezone";s:6:"+00:00";}s:7:"current";N;s:3:"end";N;s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:7;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:5;s:18:"include_start_date";b:1;s:16:"include_end_date";b:0;}"


Unserialised object:
object(DatePeriod)#5 (6) {
object(DatePeriod)#%d (%d) {
["start"]=>
object(DateTime)#6 (3) {
object(DateTime)#%d (%d) {
["date"]=>
string(26) "2012-07-01 00:00:00.000000"
["timezone_type"]=>
Expand All @@ -86,7 +88,7 @@ object(DatePeriod)#5 (6) {
["end"]=>
NULL
["interval"]=>
object(DateInterval)#4 (10) {
object(DateInterval)#%d (%d) {
["y"]=>
int(0)
["m"]=>
Expand All @@ -112,13 +114,15 @@ object(DatePeriod)#5 (6) {
int(5)
["include_start_date"]=>
bool(true)
["include_end_date"]=>
bool(false)
}


Calling __serialize manually:
array(6) {
array(%d) {
["start"]=>
object(DateTime)#7 (3) {
object(DateTime)#%d (%d) {
["date"]=>
string(26) "2012-07-01 00:00:00.000000"
["timezone_type"]=>
Expand All @@ -131,7 +135,7 @@ array(6) {
["end"]=>
NULL
["interval"]=>
object(DateInterval)#8 (10) {
object(DateInterval)#%d (%d) {
["y"]=>
int(0)
["m"]=>
Expand All @@ -157,4 +161,6 @@ array(6) {
int(5)
["include_start_date"]=>
bool(true)
["include_end_date"]=>
bool(false)
}
32 changes: 19 additions & 13 deletions ext/date/tests/DatePeriod_serialize-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ foreach ( $e as $d )
?>
--EXPECTF--
Original object:
object(DatePeriod)#4 (6) {
object(DatePeriod)#%d (%d) {
["start"]=>
object(DateTimeImmutable)#5 (3) {
object(DateTimeImmutable)#%d (%d) {
["date"]=>
string(26) "1978-12-22 09:15:00.000000"
["timezone_type"]=>
Expand All @@ -43,7 +43,7 @@ object(DatePeriod)#4 (6) {
["current"]=>
NULL
["end"]=>
object(DateTimeImmutable)#6 (3) {
object(DateTimeImmutable)#%d (%d) {
["date"]=>
string(26) "2022-04-29 15:51:56.000000"
["timezone_type"]=>
Expand All @@ -52,7 +52,7 @@ object(DatePeriod)#4 (6) {
string(13) "Europe/London"
}
["interval"]=>
object(DateInterval)#7 (10) {
object(DateInterval)#%d (%d) {
["y"]=>
int(2)
["m"]=>
Expand All @@ -78,17 +78,19 @@ object(DatePeriod)#4 (6) {
int(1)
["include_start_date"]=>
bool(true)
["include_end_date"]=>
bool(false)
}


Serialised object:
string(565) "O:10:"DatePeriod":6:{s:5:"start";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"1978-12-22 09:15:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:16:"Europe/Amsterdam";}s:7:"current";N;s:3:"end";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2022-04-29 15:51:56.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/London";}s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:2;s:1:"m";i:6;s:1:"d";i:0;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;}"
string(%d) "O:10:"DatePeriod":7:{s:5:"start";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"1978-12-22 09:15:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:16:"Europe/Amsterdam";}s:7:"current";N;s:3:"end";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2022-04-29 15:51:56.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/London";}s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:2;s:1:"m";i:6;s:1:"d";i:0;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;s:16:"include_end_date";b:0;}"


Unserialised object:
object(DatePeriod)#1 (6) {
object(DatePeriod)#%d (%d) {
["start"]=>
object(DateTimeImmutable)#2 (3) {
object(DateTimeImmutable)#%d (%d) {
["date"]=>
string(26) "1978-12-22 09:15:00.000000"
["timezone_type"]=>
Expand All @@ -99,7 +101,7 @@ object(DatePeriod)#1 (6) {
["current"]=>
NULL
["end"]=>
object(DateTimeImmutable)#8 (3) {
object(DateTimeImmutable)#%d (%d) {
["date"]=>
string(26) "2022-04-29 15:51:56.000000"
["timezone_type"]=>
Expand All @@ -108,7 +110,7 @@ object(DatePeriod)#1 (6) {
string(13) "Europe/London"
}
["interval"]=>
object(DateInterval)#9 (10) {
object(DateInterval)#%d (%d) {
["y"]=>
int(2)
["m"]=>
Expand All @@ -134,13 +136,15 @@ object(DatePeriod)#1 (6) {
int(1)
["include_start_date"]=>
bool(true)
["include_end_date"]=>
bool(false)
}


Calling __serialize manually:
array(6) {
array(%d) {
["start"]=>
object(DateTimeImmutable)#10 (3) {
object(DateTimeImmutable)#%d (%d) {
["date"]=>
string(26) "1978-12-22 09:15:00.000000"
["timezone_type"]=>
Expand All @@ -151,7 +155,7 @@ array(6) {
["current"]=>
NULL
["end"]=>
object(DateTimeImmutable)#11 (3) {
object(DateTimeImmutable)#%d (%d) {
["date"]=>
string(26) "2022-04-29 15:51:56.000000"
["timezone_type"]=>
Expand All @@ -160,7 +164,7 @@ array(6) {
string(13) "Europe/London"
}
["interval"]=>
object(DateInterval)#12 (10) {
object(DateInterval)#%d (%d) {
["y"]=>
int(2)
["m"]=>
Expand All @@ -186,6 +190,8 @@ array(6) {
int(1)
["include_start_date"]=>
bool(true)
["include_end_date"]=>
bool(false)
}


Expand Down
Loading