Skip to content

Commit

Permalink
Pass timezone to DateTime #230
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Feb 1, 2023
1 parent e2fe139 commit 185f196
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
7 changes: 5 additions & 2 deletions clickhouse_sqlalchemy/drivers/compilers/typecompiler.py
Expand Up @@ -61,11 +61,14 @@ def visit_date(self, type_, **kw):
return 'Date'

def visit_datetime(self, type_, **kw):
return 'DateTime'
if type_.timezone:
return "DateTime('%s')" % type_.timezone
else:
return 'DateTime'

def visit_datetime64(self, type_, **kw):
if type_.timezone:
return 'DateTime64(%s, \'%s\')' % (type_.precision, type_.timezone)
return "DateTime64(%s, '%s')" % (type_.precision, type_.timezone)
else:
return 'DateTime64(%s)' % type_.precision

Expand Down
9 changes: 6 additions & 3 deletions clickhouse_sqlalchemy/types/common.py
Expand Up @@ -128,17 +128,20 @@ class Date(types.Date, ClickHouseTypeEngine):
__visit_name__ = 'date'


class DateTime(types.Date, ClickHouseTypeEngine):
class DateTime(types.DateTime, ClickHouseTypeEngine):
__visit_name__ = 'datetime'

def __init__(self, timezone=None):
super(DateTime, self).__init__()
self.timezone = timezone


class DateTime64(DateTime, ClickHouseTypeEngine):
__visit_name__ = 'datetime64'

def __init__(self, precision=3, timezone=None):
self.precision = precision
self.timezone = timezone
super(DateTime64, self).__init__()
super(DateTime64, self).__init__(timezone=timezone)


class Enum(types.Enum, ClickHouseTypeEngine):
Expand Down
26 changes: 19 additions & 7 deletions tests/types/test_datetime.py
Expand Up @@ -9,18 +9,30 @@


class DateTimeCompilationTestCase(CompilationTestCase):
table = Table(
'test', CompilationTestCase.metadata(),
Column('x', types.DateTime, primary_key=True),
engines.Memory()
)

def test_create_table(self):
table = Table(
'test', CompilationTestCase.metadata(),
Column('x', types.DateTime, primary_key=True),
engines.Memory()
)

self.assertEqual(
self.compile(CreateTable(self.table)),
self.compile(CreateTable(table)),
'CREATE TABLE test (x DateTime) ENGINE = Memory'
)

def test_create_table_with_timezone(self):
table = Table(
'test', CompilationTestCase.metadata(),
Column('x', types.DateTime('Europe/Moscow'), primary_key=True),
engines.Memory()
)

self.assertEqual(
self.compile(CreateTable(table)),
"CREATE TABLE test (x DateTime('Europe/Moscow')) ENGINE = Memory"
)


@with_native_and_http_sessions
class DateTimeTestCase(BaseTestCase):
Expand Down

0 comments on commit 185f196

Please sign in to comment.