Skip to content

Commit

Permalink
Fix #90 Support for DateTimeImmutable
Browse files Browse the repository at this point in the history
  • Loading branch information
rlanvin committed Dec 9, 2020
1 parent a14dd53 commit dba3b91
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased]

## [2.2.1] - 2020-12-09

### Fixed

- Fix support for `DateTimeImmutable` [#90](https://github.com/rlanvin/php-rrule/issues/90)

## [2.2.0] - 2019-11-01

### Added
Expand Down Expand Up @@ -190,7 +196,8 @@

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

[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.2.0...HEAD
[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.2.1...HEAD
[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
[2.1.0]: https://github.com/rlanvin/php-rrule/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/rlanvin/php-rrule/compare/v2.0.0-rc1...v2.0.0
Expand Down
6 changes: 3 additions & 3 deletions src/RRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -1350,10 +1350,10 @@ public function getIterator()
$dtstart = clone $occurrence; // since DateTime is not immutable, clone to avoid any problem
// so we skip the last occurrence of the cache
if ($this->freq === self::SECONDLY) {
$dtstart->modify('+'.$this->interval.'second');
$dtstart = $dtstart->modify('+'.$this->interval.'second');
}
else {
$dtstart->modify('+1second');
$dtstart = $dtstart->modify('+1second');
}
}

Expand All @@ -1367,7 +1367,7 @@ public function getIterator()
// calculation magic at the end of the loop (when incrementing)
// to realign on first pass.
$tmp = clone $dtstart;
$tmp->modify('-'.pymod($dtstart->format('N') - $this->wkst,7).'days');
$tmp = $tmp->modify('-'.pymod($dtstart->format('N') - $this->wkst,7).'days');
list($year,$month,$day,$hour,$minute,$second) = explode(' ',$tmp->format('Y n j G i s'));
unset($tmp);
}
Expand Down
2 changes: 1 addition & 1 deletion src/RRuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function getNthOccurrenceFrom($date, $index)
* Convert any date into a DateTime object.
*
* @param mixed $date
* @return \DateTime
* @return \DateTimeInterface Returns a DateTimeImmutable if a DateTimeImmutable is passed, or DateTime otherwise
*
* @throws \InvalidArgumentException on error
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/RRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,41 @@ public function testGetRule()
$this->assertTrue($rule['DTSTART'] instanceof \DateTime);
}

/**
* Test Bug #90
* @see https://github.com/rlanvin/php-rrule/issues/90
*/
public function testDateImmutable()
{
$dtstart_immutable = \DateTimeImmutable::createFromFormat('Y-m-d H:i', '2021-01-08 08:00');
//$dtstart_mutable = \DateTime::createFromFormat('Y-m-d H:i', '2021-01-08 08:00');

$rrule = new RRule([
'BYDAY' => ['MO', 'WE', 'FR'],
'FREQ' => 'WEEKLY',
'WKST' => 'SU',
'DTSTART' => $dtstart_immutable,
]);

$start = \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01');
$end = \DateTimeImmutable::createFromFormat('Y-m-d', '2021-12-31');

$occurrences = $rrule->getOccurrencesBetween($start, $end, 10);

$this->assertEquals([
new DateTime('Friday, January 8, 2021 08:00'),
new DateTime('Monday, January 11, 2021 08:00'),
new DateTime('Wednesday, January 13, 2021 08:00'),
new DateTime('Friday, January 15, 2021 08:00'),
new DateTime('Monday, January 18, 2021 08:00'),
new DateTime('Wednesday, January 20, 2021 08:00'),
new DateTime('Friday, January 22, 2021 08:00'),
new DateTime('Monday, January 25, 2021 08:00'),
new DateTime('Wednesday, January 27, 2021 08:00'),
new DateTime('Friday, January 29, 2021 08:00')
], $occurrences, 'DateTimeImmutable produces valid results');
}

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

Expand Down

0 comments on commit dba3b91

Please sign in to comment.