Skip to content

Commit 5ef9eed

Browse files
committed
Fix #104 remove microseconds from date input
1 parent a7d7f99 commit 5ef9eed

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
## [2.3.1] - 2022-04-22
6+
7+
### Fixed
8+
9+
- Fix microseconds not always removed from dtstart, causing date comparison issues with specific date input [#104](https://github.com/rlanvin/php-rrule/issues/104)
10+
311
## [2.3.0] - 2021-10-25
412

513
### Added
@@ -215,7 +223,9 @@
215223

216224
- First release, everything before that was unversioned (`dev-master` was used).
217225

218-
[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.2.2...HEAD
226+
[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.3.1...HEAD
227+
[2.3.1]: https://github.com/rlanvin/php-rrule/compare/v2.3.0...v2.3.1
228+
[2.3.0]: https://github.com/rlanvin/php-rrule/compare/v2.2.2...v2.3.0
219229
[2.2.2]: https://github.com/rlanvin/php-rrule/compare/v2.2.1...v2.2.2
220230
[2.2.1]: https://github.com/rlanvin/php-rrule/compare/v2.2.0...v2.2.1
221231
[2.2.0]: https://github.com/rlanvin/php-rrule/compare/v2.1.0...v2.2.0

src/RRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ public function getIterator()
13911391
$timeset = $this->timeset;
13921392
}
13931393
else {
1394-
// initialize empty if it's not going to occurs on the first iteration
1394+
// initialize empty if it's not going to occur on the first iteration
13951395
if (
13961396
($this->freq >= self::HOURLY && $this->byhour && ! in_array($hour, $this->byhour))
13971397
|| ($this->freq >= self::MINUTELY && $this->byminute && ! in_array($minute, $this->byminute))
@@ -1563,7 +1563,6 @@ public function getIterator()
15631563
return;
15641564
}
15651565

1566-
// next($timeset);
15671566
if ($occurrence >= $dtstart) { // ignore occurrences before DTSTART
15681567
if ($this->count && $total >= $this->count) {
15691568
$this->total = $total;

src/RRuleTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ static public function parseDate($date)
204204
else {
205205
$date = clone $date; // avoid reference problems
206206
}
207+
208+
// ensure there is no microseconds in the DateTime object even if
209+
// the input contained microseconds, to avoid date comparison issues
210+
// (see #104)
211+
if (version_compare(PHP_VERSION, '7.1.0') < 0) {
212+
$date = new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone());
213+
}
214+
else {
215+
$date->setTime(
216+
$date->format('H'),
217+
$date->format('i'),
218+
$date->format('s'),
219+
0
220+
);
221+
}
222+
207223
return $date;
208224
}
209225
}

tests/RRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,6 +2827,24 @@ public function testDateImmutable()
28272827
], $occurrences, 'DateTimeImmutable produces valid results');
28282828
}
28292829

2830+
/**
2831+
* Test bug #104
2832+
* @see https://github.com/rlanvin/php-rrule/issues/104
2833+
*/
2834+
public function testMicrosecondsAreRemovedFromInput()
2835+
{
2836+
$dtstart = '2022-04-22 12:00:00.5';
2837+
$rule = new RRule([
2838+
'dtstart' => $dtstart,
2839+
'freq' => 'daily',
2840+
'interval' => 1,
2841+
'count' => 1
2842+
]);
2843+
$this->assertTrue($rule->occursAt('2022-04-22 12:00:00'));
2844+
$this->assertTrue($rule->occursAt('2022-04-22 12:00:00.5'));
2845+
$this->assertEquals(date_create('2022-04-22 12:00:00'), $rule[0]);
2846+
}
2847+
28302848
///////////////////////////////////////////////////////////////////////////////
28312849
// Array access and countable interfaces
28322850

0 commit comments

Comments
 (0)