Skip to content

Commit c98ec38

Browse files
committed
Fix comparison operators in neo4j.time library
1 parent 9553730 commit c98ec38

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

neo4j/time/__init__.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,19 @@ def tzinfo(self):
16571657

16581658
# OPERATIONS #
16591659

1660+
@staticmethod
1661+
def _native_time_to_ticks(native_time):
1662+
return int(3600000000000 * native_time.hour
1663+
+ 60000000000 * native_time.minute
1664+
+ NANO_SECONDS * native_time.second
1665+
+ 1000 * native_time.microsecond)
1666+
1667+
def _check_both_naive_or_tz_aware(self, other):
1668+
if (isinstance(other, (time, Time))
1669+
and ((self.tzinfo is None) ^ (other.tzinfo is None))):
1670+
raise TypeError("can't compare offset-naive and offset-aware "
1671+
"times")
1672+
16601673
def __hash__(self):
16611674
""""""
16621675
return hash(self.__ticks) ^ hash(self.tzinfo)
@@ -1666,10 +1679,7 @@ def __eq__(self, other):
16661679
if isinstance(other, Time):
16671680
return self.__ticks == other.__ticks and self.tzinfo == other.tzinfo
16681681
if isinstance(other, time):
1669-
other_ticks = (3600000000000 * other.hour
1670-
+ 60000000000 * other.minute
1671-
+ NANO_SECONDS * other.second
1672-
+ 1000 * other.microsecond)
1682+
other_ticks = self._native_time_to_ticks(other)
16731683
return self.ticks == other_ticks and self.tzinfo == other.tzinfo
16741684
return False
16751685

@@ -1679,50 +1689,50 @@ def __ne__(self, other):
16791689

16801690
def __lt__(self, other):
16811691
"""`<` comparison with :class:`.Time` or :class:`datetime.time`."""
1692+
self._check_both_naive_or_tz_aware(other)
16821693
if isinstance(other, Time):
16831694
return (self.tzinfo == other.tzinfo
16841695
and self.ticks < other.ticks)
16851696
if isinstance(other, time):
16861697
if self.tzinfo != other.tzinfo:
16871698
return False
1688-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1689-
return self.ticks < other_ticks
1699+
return self.ticks < self._native_time_to_ticks(other)
16901700
return NotImplemented
16911701

16921702
def __le__(self, other):
16931703
"""`<=` comparison with :class:`.Time` or :class:`datetime.time`."""
1704+
self._check_both_naive_or_tz_aware(other)
16941705
if isinstance(other, Time):
16951706
return (self.tzinfo == other.tzinfo
16961707
and self.ticks <= other.ticks)
16971708
if isinstance(other, time):
16981709
if self.tzinfo != other.tzinfo:
16991710
return False
1700-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1701-
return self.ticks <= other_ticks
1711+
return self.ticks <= self._native_time_to_ticks(other)
17021712
return NotImplemented
17031713

17041714
def __ge__(self, other):
17051715
"""`>=` comparison with :class:`.Time` or :class:`datetime.time`."""
1716+
self._check_both_naive_or_tz_aware(other)
17061717
if isinstance(other, Time):
17071718
return (self.tzinfo == other.tzinfo
17081719
and self.ticks >= other.ticks)
17091720
if isinstance(other, time):
17101721
if self.tzinfo != other.tzinfo:
17111722
return False
1712-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1713-
return self.ticks >= other_ticks
1723+
return self.ticks >= self._native_time_to_ticks(other)
17141724
return NotImplemented
17151725

17161726
def __gt__(self, other):
17171727
"""`>` comparison with :class:`.Time` or :class:`datetime.time`."""
1728+
self._check_both_naive_or_tz_aware(other)
17181729
if isinstance(other, Time):
17191730
return (self.tzinfo == other.tzinfo
17201731
and self.ticks >= other.ticks)
17211732
if isinstance(other, time):
17221733
if self.tzinfo != other.tzinfo:
17231734
return False
1724-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1725-
return self.ticks >= other_ticks
1735+
return self.ticks >= self._native_time_to_ticks(other)
17261736
return NotImplemented
17271737

17281738
def __copy__(self):
@@ -2203,7 +2213,8 @@ def __eq__(self, other):
22032213
`==` comparison with :class:`.DateTime` or :class:`datetime.datetime`.
22042214
"""
22052215
if isinstance(other, (DateTime, datetime)):
2206-
return self.date() == other.date() and self.time() == other.time()
2216+
return (self.date() == other.date()
2217+
and self.timetz() == other.timetz())
22072218
return False
22082219

22092220
def __ne__(self, other):
@@ -2218,7 +2229,7 @@ def __lt__(self, other):
22182229
"""
22192230
if isinstance(other, (DateTime, datetime)):
22202231
if self.date() == other.date():
2221-
return self.time() < other.time()
2232+
return self.timetz() < other.timetz()
22222233
else:
22232234
return self.date() < other.date()
22242235
return NotImplemented
@@ -2229,7 +2240,7 @@ def __le__(self, other):
22292240
"""
22302241
if isinstance(other, (DateTime, datetime)):
22312242
if self.date() == other.date():
2232-
return self.time() <= other.time()
2243+
return self.timetz() <= other.timetz()
22332244
else:
22342245
return self.date() < other.date()
22352246
return NotImplemented
@@ -2240,7 +2251,7 @@ def __ge__(self, other):
22402251
"""
22412252
if isinstance(other, (DateTime, datetime)):
22422253
if self.date() == other.date():
2243-
return self.time() >= other.time()
2254+
return self.timetz() >= other.timetz()
22442255
else:
22452256
return self.date() > other.date()
22462257
return NotImplemented
@@ -2251,7 +2262,7 @@ def __gt__(self, other):
22512262
"""
22522263
if isinstance(other, (DateTime, datetime)):
22532264
if self.date() == other.date():
2254-
return self.time() > other.time()
2265+
return self.timetz() > other.timetz()
22552266
else:
22562267
return self.date() > other.date()
22572268
return NotImplemented

0 commit comments

Comments
 (0)