Skip to content

Add support for datetime.time #834

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
hfaua opened this issue Apr 1, 2025 · 0 comments
Open

Add support for datetime.time #834

hfaua opened this issue Apr 1, 2025 · 0 comments

Comments

@hfaua
Copy link

hfaua commented Apr 1, 2025

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)
hfaua pushed a commit to hfaua/pypika that referenced this issue Apr 4, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant