Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/DateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/DateTimeImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public function testCreateFromFormatCrashOnError(): void
{
$this->expectException(\Safe\Exceptions\DatetimeException::class);
$datetime = \Safe\DateTimeImmutable::createFromFormat('lol', 'super');

Check failure on line 48 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.2)

Variable $datetime is never read.

Check failure on line 48 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.6)

Variable $datetime is never read.

Check failure on line 48 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.3)

Variable $datetime is never read.

Check failure on line 48 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.1)

Variable $datetime is never read.

Check failure on line 48 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.5)

Variable $datetime is never read.

Check failure on line 48 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.4)

Variable $datetime is never read.
}

public function testConstructorPreserveTimeAndTimezone(): void
Expand All @@ -56,6 +56,31 @@
$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');
Expand Down Expand Up @@ -109,7 +134,7 @@
{
$datetime = new \DateTimeImmutable('2000-01-01');
$safeDatime = new \Safe\DateTimeImmutable('2000-01-01');
$datetime = $datetime = $datetime->setTimestamp(12);

Check failure on line 137 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.2)

Value assigned to variable $datetime is never read before being overwritten.

Check failure on line 137 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.6)

Value assigned to variable $datetime is never read before being overwritten.

Check failure on line 137 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.3)

Value assigned to variable $datetime is never read before being overwritten.

Check failure on line 137 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.1)

Value assigned to variable $datetime is never read before being overwritten.

Check failure on line 137 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.5)

Value assigned to variable $datetime is never read before being overwritten.

Check failure on line 137 in tests/DateTimeImmutableTest.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.4)

Value assigned to variable $datetime is never read before being overwritten.
$safeDatime = $safeDatime->setTimestamp(12);

$this->assertEquals($datetime->getTimestamp(), $safeDatime->getTimestamp());
Expand Down
Loading