From 3da250d7a35bc12da4772983f3791fa23242a9e0 Mon Sep 17 00:00:00 2001 From: Nick Retallack Date: Thu, 13 Feb 2014 17:58:36 -0800 Subject: [PATCH] Fixed ArrowType to store and retrieve time consistently. SQLAlchemy's handling of time is stupid. If you hand it a datetime with an explicit time zone set, and attempt to put it in a "timestamp without time zone" field, it will convert it to your local time first. This is probably not what you want! Especially in ArrowType case, because when you retrieve the value and pass it to arrow.get, arrow assumes the time is in UTC, when it's really in local time. This leads to nonsense like this: myrecord.time = time db.session.commit() myrecord.time != time # wtf! It changed! In this commit, I make the assumption that you always want to put UTC timestamps into your database, so I convert the incoming time to UTC and then discard the time zone so SQLAlchemy wont mess with it. A future version of this type might allow you to specify what time zone you want to assume this database column actually represents, so you can convert to and from that time zone when saving and retrieving. --- sqlalchemy_utils/types/arrow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlalchemy_utils/types/arrow.py b/sqlalchemy_utils/types/arrow.py index e0c85e5..c3b17ba 100644 --- a/sqlalchemy_utils/types/arrow.py +++ b/sqlalchemy_utils/types/arrow.py @@ -63,7 +63,7 @@ class ArrowType(types.TypeDecorator, ScalarCoercible): def process_bind_param(self, value, dialect): if value: - return self._coerce(value).datetime + return self._coerce(value).to('UTC').naive return value def process_result_value(self, value, dialect):