From b7e5c6b999fbac9afb7435a912181565c4b45759 Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Mon, 2 Mar 2015 08:42:49 -0600 Subject: [PATCH] Use utcfromttimestamp workaround in SimpleDateType Also improve datetime_from_timesstamp perf by always using timedelta math. Always using timedelta math also addresses the utcfromtimestamp rounding differences noted in PYTHON-230. --- cassandra/cqltypes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cassandra/cqltypes.py b/cassandra/cqltypes.py index 9ce04b00..02602474 100644 --- a/cassandra/cqltypes.py +++ b/cassandra/cqltypes.py @@ -75,12 +75,12 @@ def unix_time_from_uuid1(u): return (u.time - 0x01B21DD213814000) / 10000000.0 +DATETIME_EPOC = datetime.datetime(1970, 1, 1) + + def datetime_from_timestamp(timestamp): - if timestamp >= 0: - dt = datetime.datetime.utcfromtimestamp(timestamp) - else: - # PYTHON-119: workaround for Windows - dt = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp) + # PYTHON-119: workaround for Windows + dt = DATETIME_EPOC + datetime.timedelta(seconds=timestamp) return dt @@ -652,7 +652,7 @@ class SimpleDateType(_CassandraType): @staticmethod def deserialize(byts, protocol_version): timestamp = SimpleDateType.seconds_per_day * (uint32_unpack(byts) - 2 ** 31) - dt = datetime.datetime.utcfromtimestamp(timestamp) + dt = datetime_from_timestamp(timestamp) return datetime.date(dt.year, dt.month, dt.day)