MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _cmp

Method _cmp

tools/python-3.11.9-amd64/Lib/datetime.py:2140–2175  ·  view source on GitHub ↗
(self, other, allow_mixed=False)

Source from the content-addressed store, hash-verified

2138 _cmperror(self, other)
2139
2140 def _cmp(self, other, allow_mixed=False):
2141 assert isinstance(other, datetime)
2142 mytz = self._tzinfo
2143 ottz = other._tzinfo
2144 myoff = otoff = None
2145
2146 if mytz is ottz:
2147 base_compare = True
2148 else:
2149 myoff = self.utcoffset()
2150 otoff = other.utcoffset()
2151 # Assume that allow_mixed means that we are called from __eq__
2152 if allow_mixed:
2153 if myoff != self.replace(fold=not self.fold).utcoffset():
2154 return 2
2155 if otoff != other.replace(fold=not other.fold).utcoffset():
2156 return 2
2157 base_compare = myoff == otoff
2158
2159 if base_compare:
2160 return _cmp((self._year, self._month, self._day,
2161 self._hour, self._minute, self._second,
2162 self._microsecond),
2163 (other._year, other._month, other._day,
2164 other._hour, other._minute, other._second,
2165 other._microsecond))
2166 if myoff is None or otoff is None:
2167 if allow_mixed:
2168 return 2 # arbitrary non-zero value
2169 else:
2170 raise TypeError("cannot compare naive and aware datetimes")
2171 # XXX What follows could be done more efficiently...
2172 diff = self - other # this will take offsets into account
2173 if diff.days < 0:
2174 return -1
2175 return diff and 1 or 0
2176
2177 def __add__(self, other):
2178 "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