Skip to content

Commit c1a8f1e

Browse files
hovaescohashhar
authored andcommitted
Support TIME WITH TIME ZONE in prepared statements
1 parent cc65a03 commit c1a8f1e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

tests/integration/test_dbapi_integration.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,23 +527,27 @@ def test_time_query_param(trino_connection):
527527

528528

529529
def test_time_with_named_time_zone_query_param(trino_connection):
530-
with pytest.raises(trino.exceptions.NotSupportedError):
531-
cur = trino_connection.cursor()
530+
cur = trino_connection.cursor()
532531

533-
params = time(16, 43, 22, 320000, tzinfo=pytz.timezone('Asia/Shanghai'))
532+
params = time(16, 43, 22, 320000, tzinfo=pytz.timezone('Asia/Shanghai'))
534533

535-
cur.execute("SELECT ?", params=(params,))
534+
cur.execute("SELECT ?", params=(params,))
535+
rows = cur.fetchall()
536+
537+
# Asia/Shanghai
538+
assert rows[0][0].tzinfo == timezone(timedelta(seconds=28800))
536539

537540

538541
def test_time_with_numeric_offset_time_zone_query_param(trino_connection):
539-
with pytest.raises(trino.exceptions.NotSupportedError):
540-
cur = trino_connection.cursor()
542+
cur = trino_connection.cursor()
541543

542-
tz = timezone(-timedelta(hours=8, minutes=0))
544+
tz = timezone(-timedelta(hours=8, minutes=0))
545+
params = time(16, 43, 22, 320000, tzinfo=tz)
543546

544-
params = time(16, 43, 22, 320000, tzinfo=tz)
547+
cur.execute("SELECT ?", params=(params,))
548+
rows = cur.fetchall()
545549

546-
cur.execute("SELECT ?", params=(params,))
550+
assert rows[0][0] == params
547551

548552

549553
def test_time(trino_connection):

trino/dbapi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from typing import Any, Dict, List, NamedTuple, Optional # NOQA for mypy types
2626
from urllib.parse import urlparse
2727

28+
import pytz
29+
2830
import trino.client
2931
import trino.exceptions
3032
import trino.logging
@@ -433,6 +435,15 @@ def _format_prepared_param(self, param):
433435
time_str = param.strftime("%H:%M:%S.%f")
434436
return "TIME '%s'" % time_str
435437

438+
if isinstance(param, datetime.time) and param.tzinfo is not None:
439+
time_str = param.strftime("%H:%M:%S.%f")
440+
# named timezones
441+
if hasattr(param.tzinfo, 'zone'):
442+
utc_offset = datetime.datetime.now(pytz.timezone(param.tzinfo.zone)).strftime('%z')
443+
return "TIME '%s %s:%s'" % (time_str, utc_offset[:3], utc_offset[3:])
444+
# offset-based timezones
445+
return "TIME '%s %s'" % (time_str, param.strftime('%Z')[3:])
446+
436447
if isinstance(param, datetime.date):
437448
date_str = param.strftime("%Y-%m-%d")
438449
return "DATE '%s'" % date_str

0 commit comments

Comments
 (0)