Skip to content

Commit

Permalink
Merge pull request #42 from wawaka/datetime_support_int
Browse files Browse the repository at this point in the history
Support supplying raw integers in DateTime columns
  • Loading branch information
xzkostyan authored Jun 29, 2018
2 parents 5928ee3 + 46a29a1 commit f378e8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class DateTimeColumn(FormatColumn):
ch_type = 'DateTime'
py_types = (datetime, )
py_types = (datetime, int)
format = 'I'

def __init__(self, timezone=None, **kwargs):
Expand All @@ -21,6 +21,11 @@ def after_read_item(self, value):
return dt.replace(tzinfo=None)

def before_write_item(self, value):
if isinstance(value, int):
# support supplying raw integers to avoid
# costly timezone conversions when using datetime
return value

if self.timezone:
# Set server's timezone for offset-naive datetime.
if value.tzinfo is None:
Expand Down
25 changes: 25 additions & 0 deletions tests/columns/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ def test_use_client_timezone(self):
inserted = self.client.execute(query, settings=settings)
self.assertEqual(inserted, [(self.dt, ), (self.dt, )])

def test_insert_integers(self):
settings = {'use_client_time_zone': True}

with self.patch_env_tz('Europe/Moscow'):
with self.create_table('a DateTime'):
self.client.execute(
'INSERT INTO test (a) VALUES', [(1530211034, )],
settings=settings
)

query = 'SELECT toUInt32(a), a FROM test'
inserted = self.emit_cli(query, use_client_time_zone=1)
self.assertEqual(inserted, '1530211034\t2018-06-28 21:37:14\n')

def test_insert_integer_bounds(self):
with self.create_table('a DateTime'):
self.client.execute(
'INSERT INTO test (a) VALUES',
[(0, ), (1, ), (1500000000, ), (2**32-1, )]
)

query = 'SELECT toUInt32(a) FROM test ORDER BY a'
inserted = self.emit_cli(query)
self.assertEqual(inserted, '0\n1\n1500000000\n4294967295\n')

@require_server_version(1, 1, 54337)
def test_datetime_with_timezone_use_server_timezone(self):
server_tz_name = self.client.execute('SELECT timezone()')[0][0]
Expand Down

0 comments on commit f378e8d

Please sign in to comment.