-
Notifications
You must be signed in to change notification settings - Fork 55
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
Fix convert_tz function in Postgresql #91
base: master
Are you sure you want to change the base?
Conversation
@x86demon please review |
@x86demon This issue tripped me up for quite a while as well. |
@mesolaries thank you for your PR and fix. Sorry for delay in the answer. |
I've checked mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','Europe/Kiev');
+-------------------------------------------------------+
| CONVERT_TZ('2004-01-01 12:00:00','UTC','Europe/Kiev') |
+-------------------------------------------------------+
| 2004-01-01 14:00:00 |
+-------------------------------------------------------+ Current implementation for PostgreSQL postgres=# SELECT "timestamp"('2004-01-01 12:00:00' AT TIME ZONE 'Europe/Kiev' AT TIME ZONE 'UTC');
timestamp
---------------------
2004-01-01 14:00:00
I'm closing the PR @mesolaries, @paulferrett thank you for paying attention. |
@x86demon Your example is not the same as current PostgreSQL implementation. Parenthesis are in wrong place. doctrine-extensions/src/Oro/ORM/Query/AST/Platform/Functions/Postgresql/ConvertTz.php Lines 22 to 24 in 67eda39
So, let's assume I want to convert datetime from UTC timezone to Europe/Kiev timezone. I will take current implementation as example. Expected output: 2004-01-01T14:00:00.000Z postgres=# SELECT "timestamp"('2004-01-01 12:00:00') AT TIME ZONE 'Europe/Kiev' AT TIME ZONE 'UTC';
timestamp
---------------------
2004-01-01T10:00:00.000Z As you can see the output is wrong. This is because |
@mesolaries you are right, I've missed parentheses position. |
@@ -1,14 +1,14 @@ | |||
- functions: | |||
- { name: "convert_tz", className: "Oro\\ORM\\Query\\AST\\Functions\\DateTime\\ConvertTz", type: "datetime" } | |||
dql: "SELECT CONVERT_TZ(f.createdAt, '+00:00', '+01:00') FROM Oro\\Entities\\Foo f WHERE f.id = 1" | |||
sql: SELECT "timestamp"(t0_.created_at) AT TIME ZONE '+01:00' AT TIME ZONE '+00:00' AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1 | |||
sql: SELECT "timestamp"(t0_.created_at) AT TIME ZONE '+00:00' AT TIME ZONE '+01:00' AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1 |
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.
Please, check expectedResult
. Query changed but result is still the same.
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.
t0_.created_at
is 2014-01-04 05:06:07. When converting from +00:00 to +01:00 expected result should be 2014-01-04 06:06:07
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 have applied your changes and run tests. Here is an output:
1) Oro\Tests\ORM\AST\Query\Functions\FunctionsTest::testDqlFunction with data set #26 (array(array('convert_tz', 'Oro\ORM\Query\AST\Functions\D...vertTz', 'datetime')), 'SELECT CONVERT_TZ(f.createdAt...id = 1', 'SELECT "timestamp"(t0_.create...id = 1', array('2014-01-04 06:06:07'))
Unexpected result for "SELECT CONVERT_TZ(f.createdAt, '+00:00', '+01:00') FROM Oro\Entities\Foo f WHERE f.id = 1"
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => '2014-01-04 06:06:07'
+ 0 => '2014-01-04 04:06:07'
)
/var/www/projects/oro-dev/doctrine-extensions/tests/Oro/Tests/ORM/AST/Query/Functions/FunctionsTest.php:48
2) Oro\Tests\ORM\AST\Query\Functions\FunctionsTest::testDqlFunction with data set #27 (array(array('convert_tz', 'Oro\ORM\Query\AST\Functions\D...vertTz', 'datetime')), 'SELECT CONVERT_TZ('2014-01-01...id = 1', 'SELECT "timestamp"('2014-01-0...id = 1', array('2014-01-01 01:00:00'))
Unexpected result for "SELECT CONVERT_TZ('2014-01-01 00:00:00', '+00:00', '+01:00') FROM Oro\Entities\Foo f WHERE f.id = 1"
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => '2014-01-01 01:00:00'
+ 0 => '2013-12-31 23:00:00'
)
/var/www/projects/oro-dev/doctrine-extensions/tests/Oro/Tests/ORM/AST/Query/Functions/FunctionsTest.php:48
3) Oro\Tests\ORM\AST\Query\Functions\FunctionsTest::testDqlFunction with data set #29 (array(array('convert_tz', 'Oro\ORM\Query\AST\Functions\D...vertTz', 'datetime')), 'SELECT CONVERT_TZ('2014-01-01...id = 1', 'SELECT "timestamp"('2014-01-0...id = 1', array('2014-01-01 02:00:00'))
Unexpected result for "SELECT CONVERT_TZ('2014-01-01 00:00:00', '+01:00', '+03:00') FROM Oro\Entities\Foo f WHERE f.id = 1"
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => '2014-01-01 02:00:00'
+ 0 => '2013-12-31 22:00:00'
)
You may check MySQL version of tests. DQL and expectedResult must be the same for MySQL and PostgreSQL and tests should pass.
Check .travis.yml for details on how to execute tests for this project
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 problem is in timezone confusion. AT TIME ZONE supports timezones in POSIX style
https://github.com/eggert/tz/blob/2018g/etcetera#L38-L44
I suggest to replace '+xx:00' type timezones to locality based timezone 'UTC', 'Europe/Kiev' for example.
Thus, the tests will be passed and less confusing.
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 ok with such change in tests if these changes will be applied for both DBs and will show that translated SQL produces same results for a given DQL.
Is +01:00
behaves differently for MySQL and PostgreSQL? If so CONVERT_TZ should have logic to prevent confusion (may be added as a separate issue)
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.
Yes, +01:00 behaves differently. I will add necessary tests.
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.
Please, do not forget about DST and issues it may bring to tests in different periods of year
AT TIME ZONE supports timezones represented in POSIX style. So, timezones that passed in +xx:xx format will be confusing.
$toTz
parameter was passed before$fromTz
. It causesconvert_tz
function to work not as expected. This pr fixes this bug.