Skip to content

Commit b2d2b66

Browse files
authored
Merge pull request #180 from Kharhamel/fix/datetimeImmutable
fixed an issue with datetimeimmutable comparaison
2 parents e7fe658 + 1dcd0e9 commit b2d2b66

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
/CONTRIBUTING.md export-ignore
55
/phpcs.xml.dist export-ignore
66
/phpstan.neon export-ignore
7-
/benchmark export-ignore
7+
/performance export-ignore
88
/generator export-ignore

generator/tests/DateTimeImmutableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,15 @@ public function testSerialize()
142142
$this->assertEquals($safeDatetime->getTimestamp(), $newDatetime->getTimestamp());
143143
$this->assertEquals($safeDatetime->getTimezone(), $newDatetime->getTimezone());
144144
}
145+
146+
public function testComparaison(): void
147+
{
148+
$safeDateTime = new \Safe\DateTimeImmutable();
149+
$phpDateTime = new \DateTimeImmutable();
150+
$timeLimit = \DateInterval::createFromDateString('2 hours');
151+
152+
$a = $safeDateTime->modify('+3 hours') < $safeDateTime->add($timeLimit);
153+
$b = $phpDateTime->modify('+3 hours') < $phpDateTime->add($timeLimit);
154+
$this->assertEquals($b, $a);
155+
}
145156
}

lib/DateTimeImmutable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class DateTimeImmutable extends \DateTimeImmutable
2828
*/
2929
public function __construct($time = 'now', $timezone = null)
3030
{
31-
parent::__construct();
31+
parent::__construct($time, $timezone);
3232
$this->innerDateTime = new parent($time, $timezone);
3333
}
3434

3535
//switch from regular datetime to safe version
3636
private static function createFromRegular(\DateTimeImmutable $datetime): self
3737
{
38-
$safeDatetime = new self();
38+
$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 '>'
3939
$safeDatetime->innerDateTime = $datetime;
4040
return $safeDatetime;
4141
}

0 commit comments

Comments
 (0)