Author: Barry Hart <barrywh...@yahoo.com>
Branch: 2634_datetime_timedelta_performance
Changeset: r93733:6513e6af0511
Date: 2017-12-31 21:40 -0500
http://bitbucket.org/pypy/pypy/changeset/6513e6af0511/

Log:    Implement __iadd__ and __isub__ on datetime objects

diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -1815,6 +1815,29 @@
 
     __radd__ = __add__
 
+    def _iadd_timedelta(self, other, factor):
+        if not isinstance(other, timedelta):
+            return NotImplemented
+        self._year, self._month, self._day, self._hour, self._minute, \
+            self._second, self._microsecond = _normalize_datetime(
+                self._year,
+                self._month,
+                self._day + other.days * factor,
+                self._hour,
+                self._minute,
+                self._second + other.seconds * factor,
+                self._microsecond + other.microseconds * factor)
+
+    def __iadd__(self, other):
+        "Increment a datetime by a timedelta."
+        self._iadd_timedelta(other, 1)
+        return self
+
+    def __isub__(self, other):
+        "Decrement a datetime by a timedelta."
+        self._iadd_timedelta(other, -1)
+        return self
+
     def __sub__(self, other):
         "Subtract two datetimes, or a datetime and a timedelta."
         if not isinstance(other, datetime):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to