Skip to content

Commit e8c658e

Browse files
committed
fixes #13773: DatePeriod does not take microseconds into account
1 parent a6b5439 commit e8c658e

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

ext/date/php_date.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,11 +1578,15 @@ static zend_result date_period_it_has_more(zend_object_iterator *iter)
15781578
php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
15791579

15801580
if (object->end) {
1581-
if (object->include_end_date) {
1582-
return object->current->sse <= object->end->sse ? SUCCESS : FAILURE;
1583-
} else {
1584-
return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
1581+
if (object->current->sse == object->end->sse) {
1582+
if (object->include_end_date) {
1583+
return object->current->us <= object->end->us ? SUCCESS : FAILURE;
1584+
} else {
1585+
return object->current->us < object->end->us ? SUCCESS : FAILURE;
1586+
}
15851587
}
1588+
1589+
return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
15861590
} else {
15871591
return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
15881592
}

ext/date/tests/date_period_include_end.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ foreach (new DatePeriod($start, $interval, $end, DatePeriod::INCLUDE_END_DATE) a
1616
2010-06-08
1717
2010-06-09
1818
2010-06-10
19-
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
DatePeriod: take microseconds into account
3+
--FILE--
4+
<?php
5+
date_default_timezone_set('UTC');
6+
$start = new DateTime('2010-06-07T01:02:03.456789');
7+
$end = new DateTime('2010-06-10T01:02:03.456789');
8+
$interval = new DateInterval('P1D');
9+
10+
echo "from " . $start->format('Y-m-d H:i:s.u') . " to " . $end->format('Y-m-d H:i:s.u') . " (exclusive)\n";
11+
foreach (new DatePeriod($start, $interval, $end) as $day) {
12+
echo $day->format('Y-m-d H:i:s.u') . "\n";
13+
}
14+
15+
echo "from " . $start->format('Y-m-d H:i:s.u') . " to " . $end->format('Y-m-d H:i:s.u') . " (inclusive)\n";
16+
foreach (new DatePeriod($start, $interval, $end, DatePeriod::INCLUDE_END_DATE) as $day) {
17+
echo $day->format('Y-m-d H:i:s.u') . "\n";
18+
}
19+
20+
$end = new DateTime('2010-06-10T01:02:03.456790');
21+
echo "from " . $start->format('Y-m-d H:i:s.u') . " to " . $end->format('Y-m-d H:i:s.u') . " (exclusive)\n";
22+
foreach (new DatePeriod($start, $interval, $end) as $day) {
23+
echo $day->format('Y-m-d H:i:s.u') . "\n";
24+
}
25+
26+
$end = new DateTime('2010-06-10T01:02:03.456788');
27+
echo "from " . $start->format('Y-m-d H:i:s.u') . " to " . $end->format('Y-m-d H:i:s.u') . " (inclusive)\n";
28+
foreach (new DatePeriod($start, $interval, $end, DatePeriod::INCLUDE_END_DATE) as $day) {
29+
echo $day->format('Y-m-d H:i:s.u') . "\n";
30+
}
31+
32+
?>
33+
--EXPECT--
34+
from 2010-06-07 01:02:03.456789 to 2010-06-10 01:02:03.456789 (exclusive)
35+
2010-06-07 01:02:03.456789
36+
2010-06-08 01:02:03.456789
37+
2010-06-09 01:02:03.456789
38+
from 2010-06-07 01:02:03.456789 to 2010-06-10 01:02:03.456789 (inclusive)
39+
2010-06-07 01:02:03.456789
40+
2010-06-08 01:02:03.456789
41+
2010-06-09 01:02:03.456789
42+
2010-06-10 01:02:03.456789
43+
from 2010-06-07 01:02:03.456789 to 2010-06-10 01:02:03.456790 (exclusive)
44+
2010-06-07 01:02:03.456789
45+
2010-06-08 01:02:03.456789
46+
2010-06-09 01:02:03.456789
47+
2010-06-10 01:02:03.456789
48+
from 2010-06-07 01:02:03.456789 to 2010-06-10 01:02:03.456788 (inclusive)
49+
2010-06-07 01:02:03.456789
50+
2010-06-08 01:02:03.456789
51+
2010-06-09 01:02:03.456789

0 commit comments

Comments
 (0)