Allow to cast as date using timezone

issue10860
review372321002
This commit is contained in:
Cédric Krier
2021-10-21 00:11:57 +02:00
parent 4134e26cd7
commit 8bbecaa817
4 changed files with 32 additions and 2 deletions
+1
View File
@@ -1,3 +1,4 @@
* Allow to cast to date with timezone
* Support create/delete attribute in view
* Skip default values when copying records
* Use global cache for Function fields in readonly transactions
+7
View File
@@ -408,6 +408,13 @@ Date
A date, represented in Python by a ``datetime.date`` instance.
Instance methods:
.. method:: Field.sql_cast(expression[, timezone])
Return the SQL expression cast as date.
If timezone is set the expression is first converted to this timezone.
DateTime
--------
+5 -2
View File
@@ -2,7 +2,7 @@
# this repository contains the full copyright notices and license terms.
import datetime
from sql.functions import Function
from sql.functions import Function, AtTimeZone
from trytond import backend
from trytond.pyson import PYSONEncoder
@@ -41,9 +41,12 @@ class Date(Field):
raise ValueError("Date field can not have time")
return super().sql_format(value)
def sql_cast(self, expression):
def sql_cast(self, expression, timezone=None):
if backend.name == 'sqlite':
return SQLite_Date(expression)
if timezone:
expression = AtTimeZone(expression, 'utc')
expression = AtTimeZone(expression, timezone)
return super(Date, self).sql_cast(expression)
+19
View File
@@ -3,11 +3,14 @@
import datetime
import unittest
from sql import Literal, Select
from sql.functions import CurrentDate
from trytond import backend
from trytond.model.exceptions import RequiredValidationError
from trytond.pool import Pool
from trytond.tests.test_tryton import activate_module, with_transaction
from trytond.transaction import Transaction
today = datetime.date(2009, 1, 1)
tomorrow = today + datetime.timedelta(1)
@@ -442,6 +445,22 @@ class FieldDateTestCase(unittest.TestCase):
'date': datetime.datetime(2009, 1, 1, 12, 0),
})
@unittest.skipIf(backend.name == 'sqlite',
"SQLite does not support timezone others than utc and localtime")
@with_transaction()
def test_sql_cast_timezone(self):
"Cast datetime to date with timezone"
Date = Pool().get('test.date')
expression = Date.date.sql_cast(
Literal(datetime.datetime(2021, 10, 14, 22, 00)),
timezone='Europe/Brussels')
cursor = Transaction().connection.cursor()
cursor.execute(*Select([expression]))
result, = cursor.fetchone()
self.assertEqual(result, datetime.date(2021, 10, 15))
def suite():
return unittest.TestLoader().loadTestsFromTestCase(FieldDateTestCase)