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

Method __sub__

Lib/_pydatetime.py:2350–2372  ·  view source on GitHub ↗

Subtract two datetimes, or a datetime and a timedelta.

(self, other)

Source from the content-addressed store, hash-verified

2348 __radd__ = __add__
2349
2350 def __sub__(self, other):
2351 "Subtract two datetimes, or a datetime and a timedelta."
2352 if not isinstance(other, datetime):
2353 if isinstance(other, timedelta):
2354 return self + -other
2355 return NotImplemented
2356
2357 days1 = self.toordinal()
2358 days2 = other.toordinal()
2359 secs1 = self._second + self._minute * 60 + self._hour * 3600
2360 secs2 = other._second + other._minute * 60 + other._hour * 3600
2361 base = timedelta(days1 - days2,
2362 secs1 - secs2,
2363 self._microsecond - other._microsecond)
2364 if self._tzinfo is other._tzinfo:
2365 return base
2366 myoff = self.utcoffset()
2367 otoff = other.utcoffset()
2368 if myoff == otoff:
2369 return base
2370 if myoff is None or otoff is None:
2371 raise TypeError("cannot mix naive and timezone-aware time")
2372 return base + otoff - myoff
2373
2374 def __hash__(self):
2375 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