@@ -1783,6 +1783,19 @@ def tzinfo(self):
1783
1783
1784
1784
# OPERATIONS #
1785
1785
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
+
1786
1799
def __hash__ (self ):
1787
1800
""""""
1788
1801
return hash (self .__ticks ) ^ hash (self .tzinfo )
@@ -1792,10 +1805,7 @@ def __eq__(self, other):
1792
1805
if isinstance (other , Time ):
1793
1806
return self .__ticks == other .__ticks and self .tzinfo == other .tzinfo
1794
1807
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 )
1799
1809
return self .ticks_ns == other_ticks and self .tzinfo == other .tzinfo
1800
1810
return False
1801
1811
@@ -1805,50 +1815,50 @@ def __ne__(self, other):
1805
1815
1806
1816
def __lt__ (self , other ):
1807
1817
"""`<` comparison with :class:`.Time` or :class:`datetime.time`."""
1818
+ self ._check_both_naive_or_tz_aware (other )
1808
1819
if isinstance (other , Time ):
1809
1820
return (self .tzinfo == other .tzinfo
1810
1821
and self .ticks_ns < other .ticks_ns )
1811
1822
if isinstance (other , time ):
1812
1823
if self .tzinfo != other .tzinfo :
1813
1824
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 )
1816
1826
return NotImplemented
1817
1827
1818
1828
def __le__ (self , other ):
1819
1829
"""`<=` comparison with :class:`.Time` or :class:`datetime.time`."""
1830
+ self ._check_both_naive_or_tz_aware (other )
1820
1831
if isinstance (other , Time ):
1821
1832
return (self .tzinfo == other .tzinfo
1822
1833
and self .ticks_ns <= other .ticks_ns )
1823
1834
if isinstance (other , time ):
1824
1835
if self .tzinfo != other .tzinfo :
1825
1836
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 )
1828
1838
return NotImplemented
1829
1839
1830
1840
def __ge__ (self , other ):
1831
1841
"""`>=` comparison with :class:`.Time` or :class:`datetime.time`."""
1842
+ self ._check_both_naive_or_tz_aware (other )
1832
1843
if isinstance (other , Time ):
1833
1844
return (self .tzinfo == other .tzinfo
1834
1845
and self .ticks_ns >= other .ticks_ns )
1835
1846
if isinstance (other , time ):
1836
1847
if self .tzinfo != other .tzinfo :
1837
1848
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 )
1840
1850
return NotImplemented
1841
1851
1842
1852
def __gt__ (self , other ):
1843
1853
"""`>` comparison with :class:`.Time` or :class:`datetime.time`."""
1854
+ self ._check_both_naive_or_tz_aware (other )
1844
1855
if isinstance (other , Time ):
1845
1856
return (self .tzinfo == other .tzinfo
1846
1857
and self .ticks_ns >= other .ticks_ns )
1847
1858
if isinstance (other , time ):
1848
1859
if self .tzinfo != other .tzinfo :
1849
1860
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 )
1852
1862
return NotImplemented
1853
1863
1854
1864
def __copy__ (self ):
@@ -2339,7 +2349,8 @@ def __eq__(self, other):
2339
2349
`==` comparison with :class:`.DateTime` or :class:`datetime.datetime`.
2340
2350
"""
2341
2351
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 ())
2343
2354
return False
2344
2355
2345
2356
def __ne__ (self , other ):
@@ -2354,7 +2365,7 @@ def __lt__(self, other):
2354
2365
"""
2355
2366
if isinstance (other , (DateTime , datetime )):
2356
2367
if self .date () == other .date ():
2357
- return self .time () < other .time ()
2368
+ return self .timetz () < other .timetz ()
2358
2369
else :
2359
2370
return self .date () < other .date ()
2360
2371
return NotImplemented
@@ -2365,7 +2376,7 @@ def __le__(self, other):
2365
2376
"""
2366
2377
if isinstance (other , (DateTime , datetime )):
2367
2378
if self .date () == other .date ():
2368
- return self .time () <= other .time ()
2379
+ return self .timetz () <= other .timetz ()
2369
2380
else :
2370
2381
return self .date () < other .date ()
2371
2382
return NotImplemented
@@ -2376,7 +2387,7 @@ def __ge__(self, other):
2376
2387
"""
2377
2388
if isinstance (other , (DateTime , datetime )):
2378
2389
if self .date () == other .date ():
2379
- return self .time () >= other .time ()
2390
+ return self .timetz () >= other .timetz ()
2380
2391
else :
2381
2392
return self .date () > other .date ()
2382
2393
return NotImplemented
@@ -2387,7 +2398,7 @@ def __gt__(self, other):
2387
2398
"""
2388
2399
if isinstance (other , (DateTime , datetime )):
2389
2400
if self .date () == other .date ():
2390
- return self .time () > other .time ()
2401
+ return self .timetz () > other .timetz ()
2391
2402
else :
2392
2403
return self .date () > other .date ()
2393
2404
return NotImplemented
0 commit comments