Skip to content

Commit

Permalink
Merge pull request #180 from Kharhamel/fix/datetimeImmutable
Browse files Browse the repository at this point in the history
fixed an issue with datetimeimmutable comparaison
  • Loading branch information
Kharhamel authored Jan 6, 2020
2 parents e7fe658 + 1dcd0e9 commit b2d2b66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/CONTRIBUTING.md export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon export-ignore
/benchmark export-ignore
/performance export-ignore
/generator export-ignore
11 changes: 11 additions & 0 deletions generator/tests/DateTimeImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,15 @@ public function testSerialize()
$this->assertEquals($safeDatetime->getTimestamp(), $newDatetime->getTimestamp());
$this->assertEquals($safeDatetime->getTimezone(), $newDatetime->getTimezone());
}

public function testComparaison(): void
{
$safeDateTime = new \Safe\DateTimeImmutable();
$phpDateTime = new \DateTimeImmutable();
$timeLimit = \DateInterval::createFromDateString('2 hours');

$a = $safeDateTime->modify('+3 hours') < $safeDateTime->add($timeLimit);
$b = $phpDateTime->modify('+3 hours') < $phpDateTime->add($timeLimit);
$this->assertEquals($b, $a);
}
}
4 changes: 2 additions & 2 deletions lib/DateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class DateTimeImmutable extends \DateTimeImmutable
*/
public function __construct($time = 'now', $timezone = null)
{
parent::__construct();
parent::__construct($time, $timezone);
$this->innerDateTime = new parent($time, $timezone);
}

//switch from regular datetime to safe version
private static function createFromRegular(\DateTimeImmutable $datetime): self
{
$safeDatetime = new self();
$safeDatetime = new self($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone()); //we need to also update the wrapper to not break the operators '<' and '>'
$safeDatetime->innerDateTime = $datetime;
return $safeDatetime;
}
Expand Down

0 comments on commit b2d2b66

Please # to comment.