jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/878200 )
Change subject: [Feature] Allow copying timezone from Timestamp ......................................................................
[Feature] Allow copying timezone from Timestamp
Bug: T325864 Change-Id: Ib6aa3c8d96baa21f21bd21030ced0719c2eaa5e2 --- M pywikibot/__init__.py M tests/wikibase_tests.py 2 files changed, 25 insertions(+), 1 deletion(-)
Approvals: Matěj Suchánek: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index 8aea5a8..22c7b38 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -574,7 +574,8 @@ precision: Union[int, str] = 14, before: int = 0, after: int = 0, timezone: int = 0, calendarmodel: Optional[str] = None, - site: Optional[DataSite] = None) -> 'WbTime': + site: Optional[DataSite] = None, + copy_timezone: bool = False) -> 'WbTime': """ Create a new WbTime object from a pywikibot.Timestamp.
@@ -590,7 +591,13 @@ :param site: The Wikibase site. If not provided, retrieves the data repository from the default site from user-config.py. Only used if calendarmodel is not given. + :param copy_timezone: Whether to copy the timezone from the Timestamp + if it has timezone information. Defaults to False to maintain + backwards compatibility. If a timezone is given, timezone + information is discarded. """ + if not timezone and timestamp.tzinfo and copy_timezone: + timezone = int(timestamp.utcoffset().total_seconds() / 60) return cls.fromTimestr(timestamp.isoformat(), precision=precision, before=before, after=after, timezone=timezone, calendarmodel=calendarmodel, diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 3bb61e5..6173dd3 100755 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -490,6 +490,13 @@ self.assertEqual(t.toTimestamp(), timestamp) self.assertEqual( t, pywikibot.WbTime.fromTimestamp(timestamp, site=repo)) + timezone = datetime.timezone(datetime.timedelta(hours=-5)) + ts = pywikibot.Timestamp(2020, 1, 1, 12, 43, 0, tzinfo=timezone) + t = pywikibot.WbTime.fromTimestamp(ts, site=repo, copy_timezone=True) + self.assertEqual(t.timezone, -5 * 60) + t = pywikibot.WbTime.fromTimestamp(ts, site=repo, copy_timezone=True, + timezone=60) + self.assertEqual(t.timezone, 60)
def test_WbTime_errors(self): """Test WbTime precision errors."""
pywikibot-commits@lists.wikimedia.org