MCPcopy Index your code
hub / github.com/RustPython/RustPython / __sub__

Method __sub__

Lib/_pydatetime.py:2307–2329  ·  view source on GitHub ↗

Subtract two datetimes, or a datetime and a timedelta.

(self, other)

Source from the content-addressed store, hash-verified

2305 __radd__ = __add__
2306
2307 def __sub__(self, other):
2308 "Subtract two datetimes, or a datetime and a timedelta."
2309 if not isinstance(other, datetime):
2310 if isinstance(other, timedelta):
2311 return self + -other
2312 return NotImplemented
2313
2314 days1 = self.toordinal()
2315 days2 = other.toordinal()
2316 secs1 = self._second + self._minute * 60 + self._hour * 3600
2317 secs2 = other._second + other._minute * 60 + other._hour * 3600
2318 base = timedelta(days1 - days2,
2319 secs1 - secs2,
2320 self._microsecond - other._microsecond)
2321 if self._tzinfo is other._tzinfo:
2322 return base
2323 myoff = self.utcoffset()
2324 otoff = other.utcoffset()
2325 if myoff == otoff:
2326 return base
2327 if myoff is None or otoff is None:
2328 raise TypeError("cannot mix naive and timezone-aware time")
2329 return base + otoff - myoff
2330
2331 def __hash__(self):
2332 if self._hashcode == -1:

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected