-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[SQLite] Support timezone information on SqlitePlatform::getDateTimeTzFormatString()
#6006
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c716da
Support timezone data on `SqlitePlatform::getDateTimeTzFormatString()`
phansys 822cc50
Add `DateTimeTzImmutableTypeTest`
phansys b7a4fa2
Update `InitializeSession` in order to fix "timestamptz" handling in …
phansys b097c77
Update `MySQL80Platform`
phansys 1092878
Remove unused imports at `DateTimeTzImmutableTypeTest`
phansys abb8f70
Disallow "fail-fast" on CI
phansys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
use function sprintf; | ||
|
||
class DateTimeTzImmutableTypeTest extends FunctionalTestCase | ||
{ | ||
private const TEST_TABLE = 'datetimetz_test'; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->iniSet('date.timezone', 'UTC'); | ||
|
||
$table = new Table(self::TEST_TABLE); | ||
$table->addColumn('id', Types::INTEGER); | ||
|
||
$table->addColumn('val', Types::DATETIMETZ_IMMUTABLE); | ||
$table->setPrimaryKey(['id']); | ||
|
||
$this->dropAndCreateTable($table); | ||
} | ||
|
||
public function testInsertAndSelect(): void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
$dateTimeTzImmutableType = Type::getType(Types::DATETIMETZ_IMMUTABLE); | ||
|
||
$id1 = 1; | ||
$value1 = new DateTimeImmutable('1986-03-22 19:45:30', new DateTimeZone('America/Argentina/Buenos_Aires')); | ||
|
||
$this->insert($id1, $value1); | ||
|
||
$res1 = $this->select($id1); | ||
|
||
$resultDateTimeTzValue = $dateTimeTzImmutableType | ||
->convertToPHPValue($res1, $platform) | ||
->setTimezone(new DateTimeZone('UTC')); | ||
|
||
self::assertInstanceOf(DateTimeImmutable::class, $resultDateTimeTzValue); | ||
self::assertSame($value1->getTimestamp(), $resultDateTimeTzValue->getTimestamp()); | ||
self::assertSame($value1->getTimestamp(), $resultDateTimeTzValue->getTimestamp()); | ||
self::assertSame('UTC', $resultDateTimeTzValue->format('T')); | ||
self::assertSame('1986-03-22T22:45:30+00:00', $resultDateTimeTzValue->format(DateTimeImmutable::ATOM)); | ||
} | ||
|
||
private function insert(int $id, DateTimeImmutable $value): void | ||
{ | ||
$result = $this->connection->insert(self::TEST_TABLE, [ | ||
'id' => $id, | ||
'val' => $value, | ||
], [ | ||
Types::INTEGER, | ||
Type::getType(Types::DATETIMETZ_IMMUTABLE), | ||
]); | ||
|
||
self::assertSame(1, $result); | ||
} | ||
|
||
private function select(int $id): string | ||
{ | ||
$value = $this->connection->fetchOne( | ||
sprintf('SELECT val FROM %s WHERE id = ?', self::TEST_TABLE), | ||
[$id], | ||
[Types::INTEGER], | ||
); | ||
|
||
self::assertIsString($value); | ||
|
||
return $value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overriding
getDateTimeTzFormatString
without overridinggetDateTimeTzTypeDeclarationSQL
(so that it does not fallback to the non-tz type) will not provide a working implementation of the type (it will actually make things worse than doing the silent fallback consistently)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid I'm missing the point here.
AFAIK, there are no specific types on MySQL for
TIMESTAMP
norDATETIME
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of the tz type is that it stores a value for a precise datetime, ensuring that when reading it again, we get the same point in time without having to agree on which timezone is implicitly used by the code.
For a tz type, we need to make sure that on insertion, we can provide a timezone and it is not ignored, but also that on reads, we can get a timezone info from the DB.
If MySQL works only with its implicit timezone on reads, it means we cannot support a tz type properly in DBAL (note that I'm not a fan of the fact that DBAL silently falls back to the non-tz type as it hides the fact that your db is not timezone aware anymore).
There is a good reason is the EcmaScript proposal for the Temporal API has separate objects representing ZonedDateTime and PlainDateTime.