@@ -1657,6 +1657,19 @@ def tzinfo(self):
1657
1657
1658
1658
# OPERATIONS #
1659
1659
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
+
1660
1673
def __hash__ (self ):
1661
1674
""""""
1662
1675
return hash (self .__ticks ) ^ hash (self .tzinfo )
@@ -1666,10 +1679,7 @@ def __eq__(self, other):
1666
1679
if isinstance (other , Time ):
1667
1680
return self .__ticks == other .__ticks and self .tzinfo == other .tzinfo
1668
1681
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 )
1673
1683
return self .ticks == other_ticks and self .tzinfo == other .tzinfo
1674
1684
return False
1675
1685
@@ -1679,50 +1689,50 @@ def __ne__(self, other):
1679
1689
1680
1690
def __lt__ (self , other ):
1681
1691
"""`<` comparison with :class:`.Time` or :class:`datetime.time`."""
1692
+ self ._check_both_naive_or_tz_aware (other )
1682
1693
if isinstance (other , Time ):
1683
1694
return (self .tzinfo == other .tzinfo
1684
1695
and self .ticks < other .ticks )
1685
1696
if isinstance (other , time ):
1686
1697
if self .tzinfo != other .tzinfo :
1687
1698
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 )
1690
1700
return NotImplemented
1691
1701
1692
1702
def __le__ (self , other ):
1693
1703
"""`<=` comparison with :class:`.Time` or :class:`datetime.time`."""
1704
+ self ._check_both_naive_or_tz_aware (other )
1694
1705
if isinstance (other , Time ):
1695
1706
return (self .tzinfo == other .tzinfo
1696
1707
and self .ticks <= other .ticks )
1697
1708
if isinstance (other , time ):
1698
1709
if self .tzinfo != other .tzinfo :
1699
1710
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 )
1702
1712
return NotImplemented
1703
1713
1704
1714
def __ge__ (self , other ):
1705
1715
"""`>=` comparison with :class:`.Time` or :class:`datetime.time`."""
1716
+ self ._check_both_naive_or_tz_aware (other )
1706
1717
if isinstance (other , Time ):
1707
1718
return (self .tzinfo == other .tzinfo
1708
1719
and self .ticks >= other .ticks )
1709
1720
if isinstance (other , time ):
1710
1721
if self .tzinfo != other .tzinfo :
1711
1722
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 )
1714
1724
return NotImplemented
1715
1725
1716
1726
def __gt__ (self , other ):
1717
1727
"""`>` comparison with :class:`.Time` or :class:`datetime.time`."""
1728
+ self ._check_both_naive_or_tz_aware (other )
1718
1729
if isinstance (other , Time ):
1719
1730
return (self .tzinfo == other .tzinfo
1720
1731
and self .ticks >= other .ticks )
1721
1732
if isinstance (other , time ):
1722
1733
if self .tzinfo != other .tzinfo :
1723
1734
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 )
1726
1736
return NotImplemented
1727
1737
1728
1738
def __copy__ (self ):
@@ -2203,7 +2213,8 @@ def __eq__(self, other):
2203
2213
`==` comparison with :class:`.DateTime` or :class:`datetime.datetime`.
2204
2214
"""
2205
2215
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 ())
2207
2218
return False
2208
2219
2209
2220
def __ne__ (self , other ):
@@ -2218,7 +2229,7 @@ def __lt__(self, other):
2218
2229
"""
2219
2230
if isinstance (other , (DateTime , datetime )):
2220
2231
if self .date () == other .date ():
2221
- return self .time () < other .time ()
2232
+ return self .timetz () < other .timetz ()
2222
2233
else :
2223
2234
return self .date () < other .date ()
2224
2235
return NotImplemented
@@ -2229,7 +2240,7 @@ def __le__(self, other):
2229
2240
"""
2230
2241
if isinstance (other , (DateTime , datetime )):
2231
2242
if self .date () == other .date ():
2232
- return self .time () <= other .time ()
2243
+ return self .timetz () <= other .timetz ()
2233
2244
else :
2234
2245
return self .date () < other .date ()
2235
2246
return NotImplemented
@@ -2240,7 +2251,7 @@ def __ge__(self, other):
2240
2251
"""
2241
2252
if isinstance (other , (DateTime , datetime )):
2242
2253
if self .date () == other .date ():
2243
- return self .time () >= other .time ()
2254
+ return self .timetz () >= other .timetz ()
2244
2255
else :
2245
2256
return self .date () > other .date ()
2246
2257
return NotImplemented
@@ -2251,7 +2262,7 @@ def __gt__(self, other):
2251
2262
"""
2252
2263
if isinstance (other , (DateTime , datetime )):
2253
2264
if self .date () == other .date ():
2254
- return self .time () > other .time ()
2265
+ return self .timetz () > other .timetz ()
2255
2266
else :
2256
2267
return self .date () > other .date ()
2257
2268
return NotImplemented
0 commit comments