0

I'd like to use the create_engine parameters to get my timezone aware data out (in python), not as a datetime.datetime, but as a string representation of the UTC time it stores.

Specifically, I have:

engine = create_engine(DB_URI,
                       convert_unicode=True,
                       json_serializer=json.dumps)

conn = engine.connect()
res = find_all(conn, myquery)

But my dates come back like this:

datetime.datetime(2020, 4, 14, 7, 30, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)

While I expect it to look more like:

2020-04-14 07:30Z

What can I change so that sqlalchemy will retrieve these values as text timestamps?

I expected the following to work as well:

PGTZ = psycopg2.extensions.new_type(
    psycopg2.extensions.DATEARRAY.values,
    'PGTZ',
    lambda value, curs: value.isoformat() if value is not None else None)

psycopg2.extensions.register_type(PGTZ)

But this had no effect.

I've consulted other references:

1 Answer 1

1

Try the cast() or type_coerce() functions?

The cast() function will use an explicit type conversion in the database, while the type_coerce() will only treat it as that type on retrieval into Python.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you help me understand how/where to use these?
In the select() part of the query? The manual has examples for both o them...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.