postgres timestamp at time zone

489 views
Skip to first unread message

espresso maker

unread,
Nov 19, 2012, 1:03:45 AM11/19/12
to sqlal...@googlegroups.com
Hi,

I have a table 'my_log' with a created_at column of type timestamp with time zone;

class Log(Base):
    __tablename__ = "my_log"
      ....
      created_at = Column(DateTime(timezone=True), nullable=False,
                        server_default=func.now())


I would like to query Log and have created_at append: at time zone '<timezone>'  without going through many hoops.


Any thoughts of implementing it cleanly?

Michael Bayer

unread,
Nov 19, 2012, 10:26:55 AM11/19/12
to sqlal...@googlegroups.com
is this psycopg2 ?  If you use Python datetime objects with the tz attribute set, it should just work, psycopg2 should do that conversion for you (haven't checked, but typically they are pretty good in this area).

Otherwise there are some SQLAlchemy routes to this, including the @compiles extension as well as the ability to associate SQL expressions with types.

If it were me, I'd advise just running your dates through a TZ conversion function on the Python side first so that they are in the TZ expected by the database beforehand.   A custom type can also do this.



--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/245CZe0qjvYJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

espresso maker

unread,
Nov 19, 2012, 2:39:49 PM11/19/12
to sqlal...@googlegroups.com
Yes, It's psycopg2.

I wasn't aware that it does the conversion itself until after I posted my question. I ended up doing what you said. :)

Thanks! 
Reply all
Reply to author
Forward
0 new messages