Xqt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316234

Change subject: [IMPR] Provide addition and subtraction methods for WbTime
......................................................................

[IMPR] Provide addition and subtraction methods for WbTime

Addition and subtraction is supported for Timedelta objects. It
works like Timestamp and datetime objects.

Some tests added.

Change-Id: I17b2b667fe70011c5be0615e5c5948e9aff3572b
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 55 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/34/316234/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 11aff5a..f810af8 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -18,6 +18,7 @@
 import sys
 import threading
 
+from copy import copy
 from decimal import Decimal
 
 if sys.version_info[0] > 2:
@@ -537,6 +538,41 @@
         return self._year > other._year or (
             self._year == other._year and self._timestamp >= other._timestamp)
 
+    def __add__(self, other):
+        """Perform addition, returning a WbTime."""
+        new = copy(self)
+        new._year = new.year
+        # leap year offset for calculating
+        new._timestamp = new._timestamp.replace(year=4800)
+        new._timestamp += other
+        diff = new._timestamp.year - 4800
+        if new._year < 0 and new._year + diff >= 0:
+            diff += 1  # skip invalid year 0
+        new._year += diff
+        if new._year >= datetime.MINYEAR and new._year <= datetime.MAXYEAR:
+            new._timestamp = new._timestamp.replace(year=new._year)
+            new._year = 0
+        else:
+            new._timestamp = new._timestamp.replace(year=2000)
+        return new
+
+    def __sub__(self, other):
+        """Perform substraction, returning a WbTime."""
+        new = copy(self)
+        new._year = new.year
+        new._timestamp = new._timestamp.replace(year=4800)
+        new._timestamp -= other
+        diff = new._timestamp.year - 4800
+        if new._year > 0 and new._year + diff <= 0:
+            diff -= 1  # skip invalid year 0
+        new._year += diff
+        if new._year >= datetime.MINYEAR and new._year <= datetime.MAXYEAR:
+            new._timestamp = new._timestamp.replace(year=new._year)
+            new._year = 0
+        else:
+            new._timestamp = new._timestamp.replace(year=2000)
+        return new
+
     def __getattr__(self, name):
         """Provide time parts from Timestamp."""
         if self.is_datetime_year() and hasattr(
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index a0d63c1..d753086 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -13,6 +13,7 @@
 import copy
 import json
 
+from datetime import timedelta
 from decimal import Decimal
 
 import pywikibot
@@ -191,6 +192,24 @@
         self.assertIsInstance(t1.toTimestamp(), pywikibot.Timestamp)
         self.assertRaises(ValueError, t2.toTimestamp)
 
+    def test_WbTime_arithmetics(self):
+        """Test WbTime arithmetics."""
+        repo = self.get_repo()
+        t = pywikibot.WbTime(site=repo, year=1)
+        t._timestamp = t.max
+        delta = timedelta(days=1)
+        t += delta
+        self.assertEqual(t.toTimestr(), '+00000010000-01-01T23:59:59Z')
+        t -= delta
+        self.assertEqual(t._timestamp, t.max)
+
+        t = pywikibot.WbTime(site=repo, year=1)
+        t._timestamp = t.min
+        t -= delta
+        self.assertEqual(t.toTimestr(), '-00000000001-12-31T00:00:00Z')
+        t += delta
+        self.assertEqual(t._timestamp, t.min)
+
     def test_WbQuantity_integer(self):
         """Test WbQuantity for integer value."""
         q = pywikibot.WbQuantity(amount=1234, error=1)

-- 
To view, visit https://gerrit.wikimedia.org/r/316234
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I17b2b667fe70011c5be0615e5c5948e9aff3572b
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <i...@gno.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to