Skip to content

Fix: Add support for datetime.time #834 #837

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pypika/terms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import inspect
import re
import uuid
from datetime import date
from datetime import (
date,
datetime,
time,
)
from enum import Enum
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -440,7 +444,7 @@ def get_formatted_value(cls, value: Any, **kwargs):
return value.get_sql(**kwargs)
if isinstance(value, Enum):
return cls.get_formatted_value(value.value, **kwargs)
if isinstance(value, date):
if isinstance(value, (date, datetime, time)):
return cls.get_formatted_value(value.isoformat(), **kwargs)
if isinstance(value, str):
value = value.replace(quote_char, quote_char * 2)
Expand Down
75 changes: 75 additions & 0 deletions pypika/tests/test_criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import (
date,
datetime,
time,
)

from pypika import (
Expand Down Expand Up @@ -75,6 +76,13 @@ def test__criterion_eq_datetime(self):
self.assertEqual("\"foo\"='2000-01-01T12:30:55'", str(c1))
self.assertEqual('"crit"."foo"=\'2000-01-01T12:30:55\'', str(c2))

def test__criterion_eq_time(self):
c1 = Field("foo") == time(12, 30, 55)
c2 = Field("foo", table=self.t).eq(time(12, 30, 55))

self.assertEqual("\"foo\"='12:30:55'", str(c1))
self.assertEqual('"crit"."foo"=\'12:30:55\'', str(c2))

def test__criterion_eq_right(self):
c1 = 1 == Field("foo")
c2 = -1 == Field("foo", table=self.t)
Expand Down Expand Up @@ -126,6 +134,13 @@ def test__criterion_ne_datetime(self):
self.assertEqual("\"foo\"<>'2000-01-01T12:30:55'", str(c1))
self.assertEqual('"crit"."foo"<>\'2000-01-01T12:30:55\'', str(c2))

def test__criterion_ne_time(self):
c1 = Field("foo") != time(12, 30, 55)
c2 = Field("foo", table=self.t).ne(time(12, 30, 55))

self.assertEqual("\"foo\"<>'12:30:55'", str(c1))
self.assertEqual('"crit"."foo"<>\'12:30:55\'', str(c2))

def test__criterion_ne_right(self):
c1 = 1 != Field("foo")
c2 = -1 != Field("foo", table=self.t)
Expand Down Expand Up @@ -156,6 +171,13 @@ def test__criterion_lt_datetime(self):
self.assertEqual("\"foo\"<'2000-01-01T12:30:55'", str(c1))
self.assertEqual('"crit"."foo"<\'2000-01-01T12:30:55\'', str(c2))

def test__criterion_lt_time(self):
c1 = Field("foo") < time(12, 30, 55)
c2 = Field("foo", table=self.t).lt(time(12, 30, 55))

self.assertEqual("\"foo\"<'12:30:55'", str(c1))
self.assertEqual('"crit"."foo"<\'12:30:55\'', str(c2))

def test__criterion_lt_right(self):
c1 = 1 > Field("foo")
c2 = -1 > Field("foo", table=self.t)
Expand Down Expand Up @@ -186,6 +208,13 @@ def test__criterion_gt_datetime(self):
self.assertEqual("\"foo\">'2000-01-01T12:30:55'", str(c1))
self.assertEqual('"crit"."foo">\'2000-01-01T12:30:55\'', str(c2))

def test__criterion_gt_time(self):
c1 = Field("foo") > time(12, 30, 55)
c2 = Field("foo", table=self.t).gt(time(12, 30, 55))

self.assertEqual("\"foo\">'12:30:55'", str(c1))
self.assertEqual('"crit"."foo">\'12:30:55\'', str(c2))

def test__criterion_gt_right(self):
c1 = 1 < Field("foo")
c2 = -1 < Field("foo", table=self.t)
Expand Down Expand Up @@ -216,6 +245,13 @@ def test__criterion_lte_datetime(self):
self.assertEqual("\"foo\"<='2000-01-01T12:30:55'", str(c1))
self.assertEqual('"crit"."foo"<=\'2000-01-01T12:30:55\'', str(c2))

def test__criterion_lte_time(self):
c1 = Field("foo") <= time(12, 30, 55)
c2 = Field("foo", table=self.t).lte(time(12, 30, 55))

self.assertEqual("\"foo\"<='12:30:55'", str(c1))
self.assertEqual('"crit"."foo"<=\'12:30:55\'', str(c2))

def test__criterion_lte_right(self):
c1 = 1 >= Field("foo")
c2 = -1 >= Field("foo", table=self.t)
Expand Down Expand Up @@ -246,6 +282,13 @@ def test__criterion_gte_datetime(self):
self.assertEqual("\"foo\">='2000-01-01T12:30:55'", str(c1))
self.assertEqual('"crit"."foo">=\'2000-01-01T12:30:55\'', str(c2))

def test__criterion_gte_time(self):
c1 = Field("foo") >= time(12, 30, 55)
c2 = Field("foo", table=self.t).gte(time(12, 30, 55))

self.assertEqual("\"foo\">='12:30:55'", str(c1))
self.assertEqual('"crit"."foo">=\'12:30:55\'', str(c2))

def test__criterion_gte_right(self):
c1 = 1 <= Field("foo")
c2 = -1 <= Field("foo", table=self.t)
Expand Down Expand Up @@ -375,6 +418,18 @@ def test__between_datetime(self):
)
self.assertEqual("\"foo\" BETWEEN '2000-01-01T00:00:00' AND '2000-12-31T23:59:59'", str(c3))

def test__between_datetime(self):
c1 = Field("foo").between(time(0, 0, 0), time(23, 59, 59))
c2 = Field("foo", table=self.t).between(time(0, 0, 0), time(23, 59, 59))
c3 = Field("foo")[time(0, 0, 0) : time(23, 59, 59)]

self.assertEqual("\"foo\" BETWEEN '00:00:00' AND '23:59:59'", str(c1))
self.assertEqual(
"\"btw\".\"foo\" BETWEEN '00:00:00' AND '23:59:59'",
str(c2),
)
self.assertEqual("\"foo\" BETWEEN '00:00:00' AND '23:59:59'", str(c3))

def test__function_between(self):
c1 = fn.Coalesce(Field("foo"), 0)[0:1]
c2 = fn.Coalesce(Field("foo", table=self.t), 0)[0:1]
Expand All @@ -392,6 +447,9 @@ def test_get_item_only_works_with_slice(self):
with self.assertRaises(TypeError):
Field("foo")[datetime(2000, 1, 1, 0, 0, 0)]

with self.assertRaises(TypeError):
Field("foo")[time(0, 0, 0)]


class IsInTests(unittest.TestCase):
t = Table("abc", alias="isin")
Expand Down Expand Up @@ -431,6 +489,13 @@ def test__in_datetime(self):
self.assertEqual("\"foo\" IN ('2000-01-01T00:00:00','2000-12-31T23:59:59')", str(c1))
self.assertEqual("\"isin\".\"foo\" IN ('2000-01-01T00:00:00','2000-12-31T23:59:59')", str(c2))

def test__in_time(self):
c1 = Field("foo").isin([time(0, 0, 0), time(23, 59, 59)])
c2 = Field("foo", table=self.t).isin([time(0, 0, 0), time(23, 59, 59)])

self.assertEqual("\"foo\" IN ('00:00:00','23:59:59')", str(c1))
self.assertEqual("\"isin\".\"foo\" IN ('00:00:00','23:59:59')", str(c2))

def test__function_isin(self):
c1 = fn.Coalesce(Field("foo"), 0).isin([0, 1])
c2 = fn.Coalesce(Field("foo", table=self.t), 0).isin([0, 1])
Expand Down Expand Up @@ -509,6 +574,16 @@ def test__notin_datetime(self):
str(c2),
)

def test__notin_time(self):
c1 = Field("foo").notin([time(0, 0, 0), time(23, 59, 59)])
c2 = Field("foo", table=self.t).notin([time(0, 0, 0), time(23, 59, 59)])

self.assertEqual("\"foo\" NOT IN ('00:00:00','23:59:59')", str(c1))
self.assertEqual(
"\"notin\".\"foo\" NOT IN ('00:00:00','23:59:59')",
str(c2),
)

def test__function_notin(self):
c1 = fn.Coalesce(Field("foo"), 0).notin([0, 1])
c2 = fn.Coalesce(Field("foo", table=self.t), 0).notin([0, 1])
Expand Down