jenkins-bot submitted this change.
[Bugfix] Normalize WbTimes sent to Wikidata
Bug: T325860
Change-Id: I178fa165ac6142a9945c77a661132676d8f498ae
---
M pywikibot/__init__.py
M pywikibot/page/_wikibase.py
M tests/wikibase_tests.py
3 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 073b1de..8202f38 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -638,7 +638,8 @@
kwargs['second'] = self.second
return type(self)(**kwargs)
- def toTimestr(self, force_iso: bool = False) -> str:
+ def toTimestr(self, force_iso: bool = False,
+ normalize: bool = False) -> str:
"""
Convert the data to a UTC date/time string.
@@ -646,8 +647,13 @@
force_iso.
:param force_iso: whether the output should be forced to ISO 8601
+ :param normalize: whether the output should be normalized (see
+ :meth:`normalize` for details)
:return: Timestamp in a format resembling ISO 8601
"""
+ if normalize:
+ return self.normalize().toTimestr(force_iso=force_iso,
+ normalize=False)
if force_iso:
return Timestamp._ISO8601Format_new.format(
self.year, max(1, self.month), max(1, self.day),
@@ -667,13 +673,15 @@
return Timestamp.fromISOformat(
self.toTimestr(force_iso=True).lstrip('+'))
- def toWikibase(self) -> Dict[str, Any]:
+ def toWikibase(self, normalize: bool = False) -> Dict[str, Any]:
"""
Convert the data to a JSON object for the Wikibase API.
+ :param normalize: Whether to normalize the WbTime object before
+ converting it to a JSON object (see :func:`normalize` for details)
:return: Wikibase JSON
"""
- json = {'time': self.toTimestr(),
+ json = {'time': self.toTimestr(normalize=normalize),
'precision': self.precision,
'after': self.after,
'before': self.before,
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index d2af3ac..eedbbbd 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1922,7 +1922,9 @@
value = self.getTarget()
elif self.type == 'commonsMedia':
value = self.getTarget().title(with_ns=False)
- elif self.type in ('globe-coordinate', 'time',
+ elif self.type == 'time':
+ value = self.getTarget().toWikibase(normalize=True)
+ elif self.type in ('globe-coordinate',
'quantity', 'monolingualtext',
'geo-shape', 'tabular-data'):
value = self.getTarget().toWikibase()
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 88a6a78..d4c2008 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -284,6 +284,15 @@
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,
+ precision=pywikibot.WbTime.PRECISION['day'])
+ self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z')
+ self.assertEqual(t.toTimestr(force_iso=True), '+2010-01-01T12:43:00Z')
+ self.assertEqual(t.toTimestr(normalize=True),
+ '+00000002010-01-01T00:00:00Z')
+ self.assertEqual(t.toTimestr(force_iso=True, normalize=True),
+ '+2010-01-01T00:00:00Z')
+
def test_WbTime_fromTimestr(self):
"""Test WbTime creation from UTC date/time string."""
repo = self.get_repo()
To view, visit change 872504. To unsubscribe, or for help writing mail filters, visit settings.