Back to Top

Thursday, February 07, 2013

Converting datetime to UTC in python

So you need to convert a python datetime object which has a timezone set ("aware" in the Python nomenclature) to an UTC one with no timezone set ("naive"), for example because NDB on GAE can't store anything else. The solution will look something like this:

date = date.astimezone(tz.tzutc()).replace(tzinfo=None)
For searcheability: the exception thrown by NDB if you fail to do this is "NotImplementedError: DatetimeProperty updated_at can only support UTC. Please derive a new Property to support alternative timezones."


Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.