Lokal Profil has uploaded a new change for review.

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

Change subject: Make WbTime from Timestamp and vice versa
......................................................................

Make WbTime from Timestamp and vice versa

To do this another parameter is introduced for WbTime.toTimestr()

Bug: T131624
Change-Id: Ia8895d29c93efd2d775144bd3baeaf347103e416
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 80 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/01/281301/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index c80c9e1..850ee43 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -417,6 +417,7 @@
                  }
 
     FORMATSTR = '{0:+012d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z'
+    FORMATSTR2 = '{0:+05d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z'
 
     _items = ('year', 'month', 'day', 'hour', 'minute', 'second',
               'precision', 'before', 'after', 'timezone', 'calendarmodel')
@@ -547,16 +548,66 @@
                    int(t[3]), int(t[4]), int(t[5]),
                    precision, before, after, timezone, calendarmodel, site)
 
-    def toTimestr(self):
+    @classmethod
+    def fromTimestamp(cls, timestamp, precision=14, before=0, after=0,
+                      timezone=0, calendarmodel=None, site=None):
+        """
+        Create a new WbTime object from a pywikibot.Timestamp.
+
+        @param timestamp: Timestamp
+        @type timestamp: pywikibot.Timestamp
+        @param precision: The unit of the precision of the time.
+        @type precision: int or str
+        @param before: Number of units after the given time it could be, if 
uncertain.
+            The unit is given by the precision.
+        @type before: int
+        @param after: Number of units before the given time it could be, if 
uncertain.
+            The unit is given by the precision.
+        @type after: int
+        @param timezone: Timezone information in minutes.
+        @type timezone: int
+        @param calendarmodel: URI identifying the calendar model
+        @type calendarmodel: str
+        @param site: The Wikibase site
+        @type site: pywikibot.site.DataSite
+        @rtype: pywikibot.WbTime
+        """
+        return cls.fromTimestr(timestamp.isoformat(), precision=precision,
+                               before=before, after=after,
+                               timezone=timezone, calendarmodel=calendarmodel,
+                               site=site)
+
+    def toTimestr(self, force_iso=False):
         """
         Convert the data to a UTC date/time string.
 
+        See fromTimestr() for differences between output with and without
+        force_iso.
+
+        @param force_iso: whether the output should be forced to ISO 8601
+        @type force_iso: bool
         @return: Timestamp in a format resembling ISO 8601
         @rtype: str
         """
+        if force_iso:
+            return self.FORMATSTR2.format(
+                self.year, max(1, self.month), max(1, self.day),
+                self.hour, self.minute, self.second)
         return self.FORMATSTR.format(self.year, self.month, self.day,
                                      self.hour, self.minute, self.second)
 
+    def toTimestamp(self):
+        """
+        Convert the data to a pywikibot.Timestamp.
+
+        @return: Timestamp
+        @rtype: pywikibot.Timestamp
+        """
+        if self.year <= 0:
+            raise Error('You cannot turn BC dates to a Timestamp')
+        return Timestamp.fromISOformat(
+            self.toTimestr(force_iso=True).lstrip('+'))
+
     def toWikibase(self):
         """
         Convert the data to a JSON object for the Wikibase API.
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 7b747e3..5037272 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -157,14 +157,39 @@
         with self.assertRaises(ValueError):
             z.precisionToDim()
 
-    def test_WbTime(self):
+    def test_WbTime_errors(self):
         """Test WbTime."""
         repo = self.get_repo()
-        t = pywikibot.WbTime(site=repo, year=2010, hour=12, minute=43)
-        self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z')
         self.assertRaises(ValueError, pywikibot.WbTime, site=repo, 
precision=15)
         self.assertRaises(ValueError, pywikibot.WbTime, site=repo, 
precision='invalid_precision')
 
+    def test_WbTime_timestr(self):
+        repo = self.get_repo()
+        t = pywikibot.WbTime(site=repo, year=2010, month=0, day=0, hour=12, 
minute=43)
+        self.assertEqual(t.toTimestr(), '+00000002010-00-00T12:43:00Z')
+        self.assertEqual(t.toTimestr(force_iso=True), '+2010-01-01T12:43:00Z')
+
+        t = pywikibot.WbTime(site=repo, year=2010, hour=12, minute=43)
+        self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z')
+        self.assertEqual(t.toTimestr(force_iso=True), '+2010-01-01T12:43:00Z')
+
+        t = pywikibot.WbTime(site=repo, year=-2010, hour=12, minute=43)
+        self.assertEqual(t.toTimestr(), '-00000002010-01-01T12:43:00Z')
+        self.assertEqual(t.toTimestr(force_iso=True), '-2010-01-01T12:43:00Z')
+
+    def test_WbTime_timestamp(self):
+        repo = self.get_repo()
+        timestamp = pywikibot.Timestamp.fromISOformat('2010-01-01T12:43:00Z')
+        t = pywikibot.WbTime(site=repo, year=2010, month=0, day=0, hour=12, 
minute=43)
+        self.assertEqual(t.toTimestamp(), timestamp)
+        # self.assertEqual(t, pywikibot.WbTime.fromTimestamp(timestamp, 
site=repo))
+
+        t = pywikibot.WbTime(site=repo, year=2010, hour=12, minute=43)
+        self.assertEqual(t.toTimestamp(), timestamp)
+
+        t = pywikibot.WbTime(site=repo, year=-2010, hour=12, minute=43)
+        self.assertRaises(pywikibot.Error, t.toTimestamp)
+
     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/281301
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia8895d29c93efd2d775144bd3baeaf347103e416
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Lokal Profil <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to