Permalink
Browse files

Fixing serialization of datetime and date types

  • Loading branch information...
stevearc committed Oct 15, 2014
1 parent 3a9ff3f commit df049aff3e1e027fae6586cfa889c630dadf1a3b
Showing with 13 additions and 9 deletions.
  1. +8 −4 flywheel/fields/types.py
  2. +5 −5 tests/test_fields.py
@@ -1,4 +1,5 @@
""" Field type definitions """
import calendar
import datetime
import functools
import six
@@ -416,10 +417,12 @@ class DateTimeType(TypeDefinition):
ddb_data_type = NUMBER
def ddb_dump(self, value):
return float(value.strftime('%s.%f'))
seconds = calendar.timegm(value.utctimetuple())
milliseconds = value.strftime('%f')
return Decimal("%d.%s" % (seconds, milliseconds))
def ddb_load(self, value):
return datetime.datetime.fromtimestamp(value)
return datetime.datetime.utcfromtimestamp(value)
register_type(DateTimeType)
@@ -431,9 +434,10 @@ class DateType(TypeDefinition):
ddb_data_type = NUMBER
def ddb_dump(self, value):
return int(value.strftime('%s'))
return calendar.timegm(value.timetuple())
def ddb_load(self, value):
return datetime.date.fromtimestamp(value)
return datetime.datetime.utcfromtimestamp(value).date()
register_type(DateType)
@@ -596,28 +596,28 @@ def test_dict_updates(self):
self.engine.save(w)
w.data['foo'] = 'bar'
w.sync()
stored_widget = self.engine.scan(PrimitiveWidget).all()[0]
stored_widget = self.engine.scan(PrimitiveWidget).one()
self.assertEquals(w.data, stored_widget.data)
def test_datetime(self):
""" Can store datetime & it gets returned as datetime """
w = PrimitiveWidget(string='a', created=datetime.utcnow())
self.engine.sync(w)
stored_widget = self.engine.scan(PrimitiveWidget).all()[0]
stored_widget = self.engine.scan(PrimitiveWidget).one()
self.assertEquals(w.created, stored_widget.created)
def test_date(self):
""" Can store date & it gets returned as date """
w = PrimitiveWidget(string='a', birthday=date.today())
self.engine.sync(w)
stored_widget = self.engine.scan(PrimitiveWidget).all()[0]
stored_widget = self.engine.scan(PrimitiveWidget).one()
self.assertEquals(w.birthday, stored_widget.birthday)
def test_decimal(self):
""" Can store decimal & it gets returned as decimal """
w = PrimitiveWidget(string='a', price=Decimal('3.50'))
self.engine.sync(w)
stored_widget = self.engine.scan(PrimitiveWidget).all()[0]
stored_widget = self.engine.scan(PrimitiveWidget).one()
self.assertEquals(w.price, stored_widget.price)
self.assertTrue(isinstance(stored_widget.price, Decimal))
@@ -628,5 +628,5 @@ def test_list_updates(self):
self.engine.save(w)
w.friends.append('Fred') # pylint: disable=E1101
w.sync()
stored_widget = self.engine.scan(PrimitiveWidget).all()[0]
stored_widget = self.engine.scan(PrimitiveWidget).one()
self.assertEquals(w.friends, stored_widget.friends)

0 comments on commit df049af

Please sign in to comment.