MCPcopy Index your code
hub / github.com/python/cpython / _cmp

Method _cmp

Lib/_pydatetime.py:2292–2327  ·  view source on GitHub ↗
(self, other, allow_mixed=False)

Source from the content-addressed store, hash-verified

2290 return NotImplemented
2291
2292 def _cmp(self, other, allow_mixed=False):
2293 assert isinstance(other, datetime)
2294 mytz = self._tzinfo
2295 ottz = other._tzinfo
2296 myoff = otoff = None
2297
2298 if mytz is ottz:
2299 base_compare = True
2300 else:
2301 myoff = self.utcoffset()
2302 otoff = other.utcoffset()
2303 # Assume that allow_mixed means that we are called from __eq__
2304 if allow_mixed:
2305 if myoff != self.replace(fold=not self.fold).utcoffset():
2306 return 2
2307 if otoff != other.replace(fold=not other.fold).utcoffset():
2308 return 2
2309 base_compare = myoff == otoff
2310
2311 if base_compare:
2312 return _cmp((self._year, self._month, self._day,
2313 self._hour, self._minute, self._second,
2314 self._microsecond),
2315 (other._year, other._month, other._day,
2316 other._hour, other._minute, other._second,
2317 other._microsecond))
2318 if myoff is None or otoff is None:
2319 if allow_mixed:
2320 return 2 # arbitrary non-zero value
2321 else:
2322 raise TypeError("cannot compare naive and aware datetimes")
2323 # XXX What follows could be done more efficiently...
2324 diff = self - other # this will take offsets into account
2325 if diff.days < 0:
2326 return -1
2327 return diff and 1 or 0
2328
2329 def __add__(self, other):
2330 "Add a datetime and a timedelta."

Callers 5

__eq__Method · 0.95
__le__Method · 0.95
__lt__Method · 0.95
__ge__Method · 0.95
__gt__Method · 0.95

Calls 5

utcoffsetMethod · 0.95
replaceMethod · 0.95
_cmpFunction · 0.70
utcoffsetMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected