Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DateTimeImmutable instead of DateTime. #775

Merged
merged 1 commit into from
Jan 3, 2019
Merged
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
8 changes: 4 additions & 4 deletions lib/Doctrine/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations\Configuration;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Doctrine\Common\EventArgs;
Expand Down Expand Up @@ -97,7 +97,7 @@ public function __construct(
$this->migrationFinder = $migrationFinder;
$this->queryWriter = $queryWriter;
$this->dependencyFactory = $dependencyFactory;
$this->migrationsColumnLength = strlen((new DateTime())->format(self::VERSION_FORMAT));
$this->migrationsColumnLength = strlen((new DateTimeImmutable())->format(self::VERSION_FORMAT));
}

public function setName(string $name) : void
Expand Down Expand Up @@ -320,7 +320,7 @@ public function createMigrationTable() : bool
public function getDateTime(string $version) : string
{
$datetime = str_replace('Version', '', $version);
$datetime = DateTime::createFromFormat(self::VERSION_FORMAT, $datetime);
$datetime = DateTimeImmutable::createFromFormat(self::VERSION_FORMAT, $datetime);

if ($datetime === false) {
return '';
Expand All @@ -331,7 +331,7 @@ public function getDateTime(string $version) : string

public function generateVersionNumber(?DateTimeInterface $now = null) : string
{
$now = $now ?: new DateTime('now', new DateTimeZone('UTC'));
$now = $now ?: new DateTimeImmutable('now', new DateTimeZone('UTC'));

return $now->format(self::VERSION_FORMAT);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/FileQueryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Migrations\Generator\FileBuilder;
use function file_put_contents;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function write(
array $queriesByVersion,
?DateTimeInterface $now = null
) : bool {
$now = $now ?? new DateTime();
$now = $now ?? new DateTimeImmutable();
Copy link
Member

@SenseException SenseException Jan 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to use new DateTimeImmutable('now', new DateTimeZone('UTC')); like above in Configuration.php? Can a timezone with daylight saving time be an issue here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should be changed to match elsewhere. Practically I don't think it would ever cause an issue though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs good timing. But this is for another time and PR to find out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will send another PR to fix that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue logged here #776


$string = $this->migrationFileBuilder
->buildMigrationFile($queriesByVersion, $direction, $now);
Expand Down