Permalink
Browse files

Datetimes get returned with a UTC timezone

  • Loading branch information...
stevearc committed Feb 23, 2015
1 parent 487e1cd commit a7c253df1510a481d7b09b3c859f201aac706f60
Showing with 27 additions and 3 deletions.
  1. +22 −1 flywheel/fields/types.py
  2. +5 −2 tests/test_fields.py
@@ -410,6 +410,27 @@ def coerce(self, value, force):
register_type(ListType)
ZERO = datetime.timedelta(0)
# https://docs.python.org/2/library/datetime.html#datetime.tzinfo.fromutc
class UTCTimezone(datetime.tzinfo):
""" UTC """
def utcoffset(self, dt):
return ZERO
def tzname(self, dt):
return 'UTC'
def dst(self, dt):
return ZERO
UTC = UTCTimezone()
class DateTimeType(TypeDefinition):
""" Datetimes, stored as a unix timestamp """
@@ -422,7 +443,7 @@ def ddb_dump(self, value):
return Decimal("%d.%s" % (seconds, milliseconds))
def ddb_load(self, value):
return datetime.datetime.utcfromtimestamp(value)
return datetime.datetime.utcfromtimestamp(value).replace(tzinfo=UTC)
register_type(DateTimeType)
@@ -10,6 +10,7 @@
from flywheel import (Field, Composite, Model, NUMBER, BINARY, STRING_SET,
NUMBER_SET, BINARY_SET, Binary, GlobalIndex, set_)
from flywheel.tests import DynamoSystemTest
from flywheel.fields.types import UTC
try:
@@ -601,10 +602,12 @@ def test_dict_updates(self):
def test_datetime(self):
""" Can store datetime & it gets returned as datetime """
w = PrimitiveWidget(string='a', created=datetime.utcnow())
now = datetime.utcnow().replace(tzinfo=UTC)
w = PrimitiveWidget(string='a', created=now)
self.engine.sync(w)
stored_widget = self.engine.scan(PrimitiveWidget).one()
self.assertEquals(w.created, stored_widget.created)
self.assertEquals(now, w.created)
self.assertEquals(now, stored_widget.created)
def test_date(self):
""" Can store date & it gets returned as date """

0 comments on commit a7c253d

Please sign in to comment.