The postgresql Time type supports times from "00:00:00" to "24:00:00" in accordance with ISO 8601. The python datetime.time class does not currently support "24:00:00" but it would be useful to have SQLAlchemy support that.
All arguments are optional. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be ints or longs, in the following ranges:
I'm using SQLAlchemy 0.6.8 and was wondering if there is a way to allow it to support the "24:00:00" midnight notation.
I've tried to make a custom type that would support it, but it seems that psycopg2 will return a datetime.time class even if I define the custom type to implement the Text type.
s = Session()c = s.connection()cast_time = lambda value, cur: Time.parse(value)TIME = c.dialect.dbapi.extensions.new_type((1083,), "TIME", cast_time)
Yes, I knew about the adapters. A coworker of mine came up with this:...s = Session()c = s.connection()cast_time = lambda value, cur: Time.parse(value)TIME = c.dialect.dbapi.extensions.new_type((1083,), "TIME", cast_time)c.dialect.dbapi.extensions.register_type(TIME)I should have mentioned that in the first place. I was wondering, what's the recommended way to modify the dialect settings globally, so they are used whenever a new engine/connection is created?
On Wednesday, April 18, 2012 10:17:43 AM UTC-4, Michael Bayer wrote:On Apr 18, 2012, at 9:36 AM, Will wrote:The postgresql Time type supports times from "00:00:00" to "24:00:00" in accordance with ISO 8601. The python datetime.time class does not currently support "24:00:00" but it would be useful to have SQLAlchemy support that.puzzled, looking at http://docs.python.org/library/datetime.html#datetime.time:All arguments are optional. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be ints or longs, in the following ranges:
- 0 <= hour < 24
- 0 <= minute < 60
- 0 <= second < 60
- 0 <= microsecond < 1000000.
?the coercion of PG's date/time fields into Python objects are a product of psycopg2. If psycopg2 isn't doing what you want here, you'd want to check with that product - psycopg2 has a comprehensive system of modifying it's typing behavior: http://initd.org/psycopg/docs/extensions.html#sql-adaptation-protocol-objectsI'm using SQLAlchemy 0.6.8 and was wondering if there is a way to allow it to support the "24:00:00" midnight notation.SQLAlchemy doesn't deal with string notations when it talks to Postgresql regarding date and time types. Psycopg2 handles the details of string formatting.I've tried to make a custom type that would support it, but it seems that psycopg2 will return a datetime.time class even if I define the custom type to implement the Text type.right, this is all psycopg2. You'd need to establish this behavior using psycopg2 only first, by registering adapters as described in the above document. Once you set that up SQLAlchemy just passes that data right through.
--
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/-/Oa13nLlwW5YJ.
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.
On Apr 18, 2012, at 10:47 AM, Will wrote:Yes, I knew about the adapters. A coworker of mine came up with this:...s = Session()c = s.connection()
cast_time = lambda value, cur: Time.parse(value)
TIME = c.dialect.dbapi.extensions.new_type((1083,), "TIME", cast_time)c.dialect.dbapi.extensions.register_type(TIME)I should have mentioned that in the first place. I was wondering, what's the recommended way to modify the dialect settings globally, so they are used whenever a new engine/connection is created?Since you're doing things that are psycopg2 specific, you could just import psycopg2:from psycopg2 import extensionsextensions.register_type(...)if you wanted to keep it local to an engine, you could do a "connect" event to add connection scope:from psycopg2 import extensions@event.listens_for(myengine, "connect")def setup_time_type(dbapi_conn, conn_rec):extensions.register_type(TIME, dbapi_conn)