diff --git a/lib/DateTimeImmutable.php b/lib/DateTimeImmutable.php index 8a240e8d..99f74101 100644 --- a/lib/DateTimeImmutable.php +++ b/lib/DateTimeImmutable.php @@ -24,8 +24,8 @@ class DateTimeImmutable extends \DateTimeImmutable */ public function __construct($time = 'now', $timezone = null) { - parent::__construct($time, $timezone); $this->innerDateTime = new parent($time, $timezone); + parent::__construct($this->innerDateTime->format('Y-m-d H:i:s.u'), $this->innerDateTime->getTimezone()); } //switch between regular datetime and safe version diff --git a/tests/DateTimeImmutableTest.php b/tests/DateTimeImmutableTest.php index a6c27cfd..9ad4226f 100644 --- a/tests/DateTimeImmutableTest.php +++ b/tests/DateTimeImmutableTest.php @@ -56,6 +56,31 @@ public function testConstructorPreserveTimeAndTimezone(): void $this->assertEquals($timezone, $datetime->getTimezone()); } + public function testConstructorInnerAndOuterDatesAreEqual(): void + { + for ($i = 0; $i < 1000; $i++) { + $datetime = new \Safe\DateTimeImmutable(); + $this->assertSame( + self::nativeFormat($datetime, 'Y-m-d H:i:s.u'), + $datetime->getInnerDateTime()->format('Y-m-d H:i:s.u') + ); + } + } + + public function testConstructorPreservesTimezoneFromTimeString(): void + { + foreach (['@1700000000', '2020-01-01T00:00:00+02:00'] as $time) { + $datetime = new \Safe\DateTimeImmutable($time); + $this->assertSame(self::nativeFormat($datetime, 'U P'), $datetime->getInnerDateTime()->format('U P')); + } + } + + private static function nativeFormat(\DateTimeImmutable $datetime, string $format): string + { + // bypass \Safe\DateTimeImmutable::format() to read the parent's own value + return (new \ReflectionMethod(\DateTimeImmutable::class, 'format'))->invoke($datetime, $format); + } + public function testCreateFromFormatPreserveTimeAndTimezone(): void { $timezone = new \DateTimeZone('Pacific/Chatham');