we do this (for PG and SQL Server): class utcnow(expression.FunctionElement): key = 'utcnow' type = DateTime()
@compiles(utcnow, 'postgresql') def _pg_utcnow(element, compiler, **kw): return "TIMEZONE('utc', CURRENT_TIMESTAMP)" @compiles(utcnow, 'mssql') def _ms_utcnow(element, compiler, **kw): return "GETUTCDATE()" you then use utcnow() as your SQL expression. just remembered i put it in the docs too: http://docs.sqlalchemy.org/en/rel_0_8/core/compiler.html#utc-timestamp-function On Jun 5, 2013, at 4:51 PM, Owen M <owenm...@gmail.com> wrote: > Was hunting around for how to do this, and couldn't find any examples, but > eventually figured it out. > > Goal is to have all dates coming out of Postgres to be in UTC, regardless of > the DBA's timezone configuration. > > Key piece of code is as follows: > > import sqlalchemy > def set_utc_on_connect(dbapi_con, con_record): > c = dbapi_con.cursor() > c.execute("SET TIME ZONE UTC") > c.close() > sqlalchemy.event.listen(sqlalchemy.pool.Pool, 'connect', set_utc_on_connect) > > Now all connections in the pool will have their timezone set to UTC, so all > queries will return UTC for datetime objects with timezone specified. > > Just posting it here incase someone else needed the info. > > Cheers, > ~Owen > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.