diff --git a/generator/tests/DateTimeImmutableTest.php b/generator/tests/DateTimeImmutableTest.php index 4ae5ad38..b4c49e7e 100644 --- a/generator/tests/DateTimeImmutableTest.php +++ b/generator/tests/DateTimeImmutableTest.php @@ -153,4 +153,16 @@ public function testComparaison(): void $b = $phpDateTime->modify('+3 hours') < $phpDateTime->add($timeLimit); $this->assertEquals($b, $a); } + + public function testEquals(): void + { + $phpDateTime = new \DateTimeImmutable(); + + $safeDateTime1 = \Safe\DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $phpDateTime->format('Y-m-d H:i:s.u')); + $safeDateTime2 = new \Safe\DateTimeImmutable($safeDateTime1->format('Y-m-d H:i:s.u')); + + $this->assertEquals($phpDateTime, $safeDateTime1); + $this->assertEquals($phpDateTime, $safeDateTime2); + $this->assertEquals($safeDateTime1, $safeDateTime2); + } } \ No newline at end of file diff --git a/lib/DateTime.php b/lib/DateTime.php index 40cb7595..b3778d4b 100644 --- a/lib/DateTime.php +++ b/lib/DateTime.php @@ -13,7 +13,7 @@ class DateTime extends \DateTime //switch from regular datetime to safe version private static function createFromRegular(\DateTime $datetime): self { - return new self($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone()); + return new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); } /** diff --git a/lib/DateTimeImmutable.php b/lib/DateTimeImmutable.php index a0956492..948b8f0f 100644 --- a/lib/DateTimeImmutable.php +++ b/lib/DateTimeImmutable.php @@ -35,7 +35,7 @@ public function __construct($time = 'now', $timezone = null) //switch from regular datetime to safe version private static function createFromRegular(\DateTimeImmutable $datetime): 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 = new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); //we need to also update the wrapper to not break the operators '<' and '>' $safeDatetime->innerDateTime = $datetime; return $safeDatetime; }