-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-add and naive datetimes not offsetting timezone correctly #517
Comments
Perhaps a hint to the bug here: >>> datetime.datetime.utcnow()
datetime.datetime(2020, 8, 21, 17, 57, 23, 479409)
>>> datetime.datetime.utcnow().astimezone(datetime.timezone.utc)
datetime.datetime(2020, 8, 21, 21, 57, 24, 355568, tzinfo=datetime.timezone.utc) Similar logic is in both |
Here's another implementation of the test class above that parametrizes the inputs, and also makes it easier to subclass it in order to test a fixed version of the DateTimeProperty: from datetime import datetime, timedelta, timezone
import pytest
from google.cloud import ndb
class TestDateTimeTimezone:
DATE_TIME_PROPERTY = ndb.DateTimeProperty
@classmethod
def _model(cls):
class ModelWithDateTime(ndb.Model):
dt = cls.DATE_TIME_PROPERTY(auto_now_add=True, tzinfo=timezone.utc)
return ModelWithDateTime
@staticmethod
def _now():
return datetime.now(timezone.utc)
@classmethod
def _assert_datetime_is_current(cls, dt):
now = cls._now()
assert abs(dt - cls._now()) < timedelta(seconds=3), \
f"Provided datetime {dt} is not close to current datetime {now}"
@pytest.mark.parametrize("kwarg_factory", [
lambda: dict(),
lambda: dict(dt=datetime.utcnow()),
lambda: dict(dt=datetime.now(timezone.utc)),
])
def test_ndb_datetime_correct(self, kwarg_factory):
entity = self._model()(**kwarg_factory())
with ndb.Client().context():
entity.put()
self._assert_datetime_is_current(entity.dt)
entity.key.get()
self._assert_datetime_is_current(entity.dt) This is its test output:
|
Here is the fix using a subclassed DateTimeProperty: class DateTimeProperty(ndb.DateTimeProperty):
"""This class fixes a bug with timezone offsets when using the auto_add
kwarg feature, or when setting a naive datetime.
https://github.com/googleapis/python-ndb/issues/517
"""
def _to_base_type(self, value):
if self._tzinfo is not None:
import pytz
if value.tzinfo is not None:
return value.astimezone(pytz.utc)
else:
return value.replace(tzinfo=pytz.utc) Here is the test class subclasses from the test class in my last comment: class TestDateTimePropertyFix(test_ndb.TestDateTimeTimezone):
DATE_TIME_PROPERTY = properties.DateTimeProperty |
Here's some unit tests that check retrieved datetime values from the DateTimeProperty:
Here is the test output when running in timezone America/New_York during DST (i.e., UTC -4):
The first two tests, auto-add and manually set naive datetime, fail. They both seem to do a double offset of 4 hours from the local system timezone. The last test is a manually set aware datetime, and it passes.
The text was updated successfully, but these errors were encountered: