Skip to content

Commit c117049

Browse files
committed
Fix comparison operators in neo4j.time library
1 parent 1d2748a commit c117049

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
@@ -1783,6 +1783,19 @@ def tzinfo(self):
17831783

17841784
# OPERATIONS #
17851785

1786+
@staticmethod
1787+
def _native_time_to_ticks(native_time):
1788+
return int(3600000000000 * native_time.hour
1789+
+ 60000000000 * native_time.minute
1790+
+ NANO_SECONDS * native_time.second
1791+
+ 1000 * native_time.microsecond)
1792+
1793+
def _check_both_naive_or_tz_aware(self, other):
1794+
if (isinstance(other, (time, Time))
1795+
and ((self.tzinfo is None) ^ (other.tzinfo is None))):
1796+
raise TypeError("can't compare offset-naive and offset-aware "
1797+
"times")
1798+
17861799
def __hash__(self):
17871800
""""""
17881801
return hash(self.__ticks) ^ hash(self.tzinfo)
@@ -1792,10 +1805,7 @@ def __eq__(self, other):
17921805
if isinstance(other, Time):
17931806
return self.__ticks == other.__ticks and self.tzinfo == other.tzinfo
17941807
if isinstance(other, time):
1795-
other_ticks = (3600000000000 * other.hour
1796-
+ 60000000000 * other.minute
1797-
+ NANO_SECONDS * other.second
1798-
+ 1000 * other.microsecond)
1808+
other_ticks = self._native_time_to_ticks(other)
17991809
return self.ticks_ns == other_ticks and self.tzinfo == other.tzinfo
18001810
return False
18011811

@@ -1805,50 +1815,50 @@ def __ne__(self, other):
18051815

18061816
def __lt__(self, other):
18071817
"""`<` comparison with :class:`.Time` or :class:`datetime.time`."""
1818+
self._check_both_naive_or_tz_aware(other)
18081819
if isinstance(other, Time):
18091820
return (self.tzinfo == other.tzinfo
18101821
and self.ticks_ns < other.ticks_ns)
18111822
if isinstance(other, time):
18121823
if self.tzinfo != other.tzinfo:
18131824
return False
1814-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1815-
return self.ticks_ns < other_ticks
1825+
return self.ticks_ns < self._native_time_to_ticks(other)
18161826
return NotImplemented
18171827

18181828
def __le__(self, other):
18191829
"""`<=` comparison with :class:`.Time` or :class:`datetime.time`."""
1830+
self._check_both_naive_or_tz_aware(other)
18201831
if isinstance(other, Time):
18211832
return (self.tzinfo == other.tzinfo
18221833
and self.ticks_ns <= other.ticks_ns)
18231834
if isinstance(other, time):
18241835
if self.tzinfo != other.tzinfo:
18251836
return False
1826-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1827-
return self.ticks_ns <= other_ticks
1837+
return self.ticks_ns <= self._native_time_to_ticks(other)
18281838
return NotImplemented
18291839

18301840
def __ge__(self, other):
18311841
"""`>=` comparison with :class:`.Time` or :class:`datetime.time`."""
1842+
self._check_both_naive_or_tz_aware(other)
18321843
if isinstance(other, Time):
18331844
return (self.tzinfo == other.tzinfo
18341845
and self.ticks_ns >= other.ticks_ns)
18351846
if isinstance(other, time):
18361847
if self.tzinfo != other.tzinfo:
18371848
return False
1838-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1839-
return self.ticks_ns >= other_ticks
1849+
return self.ticks_ns >= self._native_time_to_ticks(other)
18401850
return NotImplemented
18411851

18421852
def __gt__(self, other):
18431853
"""`>` comparison with :class:`.Time` or :class:`datetime.time`."""
1854+
self._check_both_naive_or_tz_aware(other)
18441855
if isinstance(other, Time):
18451856
return (self.tzinfo == other.tzinfo
18461857
and self.ticks_ns >= other.ticks_ns)
18471858
if isinstance(other, time):
18481859
if self.tzinfo != other.tzinfo:
18491860
return False
1850-
other_ticks = 3600 * other.hour + 60 * other.minute + other.second + (other.microsecond / 1000000)
1851-
return self.ticks_ns >= other_ticks
1861+
return self.ticks_ns >= self._native_time_to_ticks(other)
18521862
return NotImplemented
18531863

18541864
def __copy__(self):
@@ -2339,7 +2349,8 @@ def __eq__(self, other):
23392349
`==` comparison with :class:`.DateTime` or :class:`datetime.datetime`.
23402350
"""
23412351
if isinstance(other, (DateTime, datetime)):
2342-
return self.date() == other.date() and self.time() == other.time()
2352+
return (self.date() == other.date()
2353+
and self.timetz() == other.timetz())
23432354
return False
23442355

23452356
def __ne__(self, other):
@@ -2354,7 +2365,7 @@ def __lt__(self, other):
23542365
"""
23552366
if isinstance(other, (DateTime, datetime)):
23562367
if self.date() == other.date():
2357-
return self.time() < other.time()
2368+
return self.timetz() < other.timetz()
23582369
else:
23592370
return self.date() < other.date()
23602371
return NotImplemented
@@ -2365,7 +2376,7 @@ def __le__(self, other):
23652376
"""
23662377
if isinstance(other, (DateTime, datetime)):
23672378
if self.date() == other.date():
2368-
return self.time() <= other.time()
2379+
return self.timetz() <= other.timetz()
23692380
else:
23702381
return self.date() < other.date()
23712382
return NotImplemented
@@ -2376,7 +2387,7 @@ def __ge__(self, other):
23762387
"""
23772388
if isinstance(other, (DateTime, datetime)):
23782389
if self.date() == other.date():
2379-
return self.time() >= other.time()
2390+
return self.timetz() >= other.timetz()
23802391
else:
23812392
return self.date() > other.date()
23822393
return NotImplemented
@@ -2387,7 +2398,7 @@ def __gt__(self, other):
23872398
"""
23882399
if isinstance(other, (DateTime, datetime)):
23892400
if self.date() == other.date():
2390-
return self.time() > other.time()
2401+
return self.timetz() > other.timetz()
23912402
else:
23922403
return self.date() > other.date()
23932404
return NotImplemented

0 commit comments

Comments
 (0)