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

Method __sub__

tools/python-3.11.9-amd64/Lib/datetime.py:2198–2220  ·  view source on GitHub ↗

Subtract two datetimes, or a datetime and a timedelta.

(self, other)

Source from the content-addressed store, hash-verified

2196 __radd__ = __add__
2197
2198 def __sub__(self, other):
2199 "Subtract two datetimes, or a datetime and a timedelta."
2200 if not isinstance(other, datetime):
2201 if isinstance(other, timedelta):
2202 return self + -other
2203 return NotImplemented
2204
2205 days1 = self.toordinal()
2206 days2 = other.toordinal()
2207 secs1 = self._second + self._minute * 60 + self._hour * 3600
2208 secs2 = other._second + other._minute * 60 + other._hour * 3600
2209 base = timedelta(days1 - days2,
2210 secs1 - secs2,
2211 self._microsecond - other._microsecond)
2212 if self._tzinfo is other._tzinfo:
2213 return base
2214 myoff = self.utcoffset()
2215 otoff = other.utcoffset()
2216 if myoff == otoff:
2217 return base
2218 if myoff is None or otoff is None:
2219 raise TypeError("cannot mix naive and timezone-aware time")
2220 return base + otoff - myoff
2221
2222 def __hash__(self):
2223 if self._hashcode == -1:

Callers

nothing calls this directly

Calls 4

utcoffsetMethod · 0.95
timedeltaClass · 0.85
toordinalMethod · 0.80
utcoffsetMethod · 0.45

Tested by

no test coverage detected