Skip to content

Commit

Permalink
Fix #104 remove microseconds from date input
Browse files Browse the repository at this point in the history
  • Loading branch information
rlanvin committed Apr 22, 2022
1 parent a7d7f99 commit 5ef9eed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [Unreleased]

## [2.3.1] - 2022-04-22

### Fixed

- Fix microseconds not always removed from dtstart, causing date comparison issues with specific date input [#104](https://github.com/rlanvin/php-rrule/issues/104)

## [2.3.0] - 2021-10-25

### Added
Expand Down Expand Up @@ -215,7 +223,9 @@

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

[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.2.2...HEAD
[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.3.1...HEAD
[2.3.1]: https://github.com/rlanvin/php-rrule/compare/v2.3.0...v2.3.1
[2.3.0]: https://github.com/rlanvin/php-rrule/compare/v2.2.2...v2.3.0
[2.2.2]: https://github.com/rlanvin/php-rrule/compare/v2.2.1...v2.2.2
[2.2.1]: https://github.com/rlanvin/php-rrule/compare/v2.2.0...v2.2.1
[2.2.0]: https://github.com/rlanvin/php-rrule/compare/v2.1.0...v2.2.0
Expand Down
3 changes: 1 addition & 2 deletions src/RRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ public function getIterator()
$timeset = $this->timeset;
}
else {
// initialize empty if it's not going to occurs on the first iteration
// initialize empty if it's not going to occur on the first iteration
if (
($this->freq >= self::HOURLY && $this->byhour && ! in_array($hour, $this->byhour))
|| ($this->freq >= self::MINUTELY && $this->byminute && ! in_array($minute, $this->byminute))
Expand Down Expand Up @@ -1563,7 +1563,6 @@ public function getIterator()
return;
}

// next($timeset);
if ($occurrence >= $dtstart) { // ignore occurrences before DTSTART
if ($this->count && $total >= $this->count) {
$this->total = $total;
Expand Down
16 changes: 16 additions & 0 deletions src/RRuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ static public function parseDate($date)
else {
$date = clone $date; // avoid reference problems
}

// ensure there is no microseconds in the DateTime object even if
// the input contained microseconds, to avoid date comparison issues
// (see #104)
if (version_compare(PHP_VERSION, '7.1.0') < 0) {
$date = new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone());
}
else {
$date->setTime(
$date->format('H'),
$date->format('i'),
$date->format('s'),
0
);
}

return $date;
}
}
18 changes: 18 additions & 0 deletions tests/RRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2827,6 +2827,24 @@ public function testDateImmutable()
], $occurrences, 'DateTimeImmutable produces valid results');
}

/**
* Test bug #104
* @see https://github.com/rlanvin/php-rrule/issues/104
*/
public function testMicrosecondsAreRemovedFromInput()
{
$dtstart = '2022-04-22 12:00:00.5';
$rule = new RRule([
'dtstart' => $dtstart,
'freq' => 'daily',
'interval' => 1,
'count' => 1
]);
$this->assertTrue($rule->occursAt('2022-04-22 12:00:00'));
$this->assertTrue($rule->occursAt('2022-04-22 12:00:00.5'));
$this->assertEquals(date_create('2022-04-22 12:00:00'), $rule[0]);
}

///////////////////////////////////////////////////////////////////////////////
// Array access and countable interfaces

Expand Down

0 comments on commit 5ef9eed

Please # to comment.