Skip to content

Add support for datetime.time #834

Open
@hfaua

Description

@hfaua

Consider following code:

import datetime
from pypika import Query
from pypika import Table

d = datetime.date(2000, 1, 1)
dt = datetime.datetime(2022, 1, 1, 1, 1, 1)
ti = datetime.time(1, 1, 1)

t = Table("t")

q1 = Query.into(t).insert(d)
q2 = Query.into(t).insert(dt)
q3 = Query.into(t).insert(ti)

>>> q1
INSERT INTO "t" VALUES ('2000-01-01')
>>> q2
INSERT INTO "t" VALUES ('2022-01-01T01:01:01')
>>> q3
INSERT INTO "t" VALUES (01:01:01)

As you can see, the conversion for datetime.date and datetime.datetime is correct - values are converted using datetime.date.isoformat and datetime.datetime.isoformat and enclosed with single quotes. Unfortunately, values for datetime.time are not enclosed with quotes which leads to invalid queries. This is due to the fact that in ValueWrapper values datetime.date and datetime.datetime fall under isinstance(value, date) which does the proper conversion but datetime.time falls under str(value).

My proposal is to enhance the condition to also cover datetime.time:

pypika/terms.py:443

if isinstance(value, date) or isinstance(value. time):
    return cls.get_formatted_value(value.isoformat(), **kwargs)

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