From 2766377dfa9c66212c23c3d043c0d9ec6a3380f8 Mon Sep 17 00:00:00 2001 From: Tyler Hobbs Date: Wed, 18 Dec 2013 12:43:53 -0600 Subject: [PATCH] Preserve milli-precision in non-prepared datetimes When a datetime was used for a 'timestamp' value in a non-prepared statement, all sub-second precision was lost. After this change, millisecond level precision will be kept. --- cassandra/decoder.py | 4 +++- tests/integration/test_types.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cassandra/decoder.py b/cassandra/decoder.py index 5d487e1b..f9bfa16c 100644 --- a/cassandra/decoder.py +++ b/cassandra/decoder.py @@ -1,4 +1,5 @@ from binascii import hexlify +import calendar from collections import namedtuple import datetime import logging @@ -788,7 +789,8 @@ def cql_encode_object(val): def cql_encode_datetime(val): - return "'%s'" % val.strftime('%Y-%m-%d %H:%M:%S-0000') + timestamp = calendar.timegm(val.utctimetuple()) + return str(long(timestamp * 1e3 + getattr(val, 'microsecond', 0) / 1e3)) def cql_encode_date(val): diff --git a/tests/integration/test_types.py b/tests/integration/test_types.py index ed0e1fa3..c8c808b5 100644 --- a/tests/integration/test_types.py +++ b/tests/integration/test_types.py @@ -131,7 +131,7 @@ class TypeTests(unittest.TestCase): v1_uuid = uuid1() v4_uuid = uuid4() - mydatetime = datetime(2013, 1, 1, 1, 1, 1) + mydatetime = datetime(2013, 12, 31, 23, 59, 59, 999000) params = [ "sometext",