Skip to content

Age function record data type misinterpreted #643

@alexhenman

Description

@alexhenman

There seems to be some peculiarity with the way the result from calling the age function is being interpreted. The postgres documentation suggests that an interval is returned but it seems something unusual is happening in how this is getting parsed by psycopg.

Minimal steps for replicating are this:

connection = psycopg.connect(...)
cursor = connection.cursor()

# This returns datetime.timedelta(days=6) when it should be 6 months and 6 days
cursor.execute("SELECT age('2020-07-07 00:00:00'::timestamp, '2020-01-01 00:00:00'::timestamp);").fetchone()[0]

# Whereas just subtracting the timestamps results in the right output: datetime.timedelta(days=188)
cursor.execute("SELECT '2020-07-07 00:00:00'::timestamp - '2020-01-01 00:00:00'::timestamp;").fetchone()[0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions