Fix DateType marshalling
This commit is contained in:
@@ -33,6 +33,8 @@ from cassandra.marshal import (int8_pack, int8_unpack, uint16_pack, uint16_unpac
|
||||
|
||||
apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.'
|
||||
|
||||
_number_types = frozenset((int, long, float))
|
||||
|
||||
def trim_if_startswith(s, prefix):
|
||||
if s.startswith(prefix):
|
||||
return s[len(prefix):]
|
||||
@@ -473,9 +475,18 @@ class DateType(_CassandraType):
|
||||
return datetime.utcfromtimestamp(int64_unpack(byts) / 1000.0)
|
||||
|
||||
@staticmethod
|
||||
def serialize(timestamp):
|
||||
return int64_pack(timestamp * 1000)
|
||||
def serialize(v):
|
||||
try:
|
||||
converted = calendar.timegm(v.utctimetuple())
|
||||
converted = converted * 1e3 + getattr(v, 'microsecond', 0) / 1e3
|
||||
except AttributeError:
|
||||
# Ints and floats are valid timestamps too
|
||||
if type(v) not in _number_types:
|
||||
raise TypeError('DateType arguments must be a datetime or timestamp')
|
||||
|
||||
converted = v * 1e3
|
||||
|
||||
return int64_pack(long(converted))
|
||||
|
||||
class TimeUUIDType(DateType):
|
||||
typename = 'timeuuid'
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from uuid import UUID
|
||||
|
||||
@@ -15,7 +16,7 @@ marshalled_value_pairs = (
|
||||
('\x7f\xff\xff\xff\xff\xff\xff\xff', 'CounterColumnType', 9223372036854775807),
|
||||
('\x80\x00\x00\x00\x00\x00\x00\x00', 'CounterColumnType', -9223372036854775808),
|
||||
('', 'CounterColumnType', None),
|
||||
('\x00\x00\x013\x7fb\xeey', 'DateType', 1320692149.881),
|
||||
('\x00\x00\x013\x7fb\xeey', 'DateType', datetime(2011, 11, 7, 18, 55, 49, 881000)),
|
||||
('', 'DateType', None),
|
||||
('\x00\x00\x00\r\nJ\x04"^\x91\x04\x8a\xb1\x18\xfe', 'DecimalType', Decimal('1243878957943.1234124191998')),
|
||||
('\x00\x00\x00\x06\xe5\xde]\x98Y', 'DecimalType', Decimal('-112233.441191')),
|
||||
|
Reference in New Issue
Block a user