J-bob
J-bob

Reputation: 9126

getting SQLAlchemy to return times in UTC

I can't figure out how to get SQLAlchemy to return times in UTC. I have a model with a datetime field with timezone=True. I know that Postgres stores all timezones internally in UTC, but when the date comes back out it's shifted to the timezone I'm. The python datetime object has it's tzinfo set to psycopg2.tz.FixedOffsetTimezone(offset=-240, name=None) when SQLAlchemy retrieves it from Postgres. I know I could just convert to UTC once I get the date out of SQLalchemy, but that just sounds tedious since I'll be using datetimes everywhere in my code.

Upvotes: 2

Views: 1064

Answers (2)

Spencer
Spencer

Reputation: 727

Are you sure the postgres database doesn't have a timezone set? Log in to postgres and run:

test_db=> show timezone;
TimeZone 
---------
US/Central

This will cause all times output from SQLAlchemy to have central timezone. You want to make sure this value is something like this instead:

test_db=> show timezone;
 TimeZone
----------
 UTC

Upvotes: 1

bhinesley
bhinesley

Reputation: 2289

By default, SQLAlchemy will return times in UTC. You should remove timezone=True (or set it to False).

Upvotes: 0

Related Questions