jenkins-bot submitted this change.

View Change


Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
[Bugfix] Add timezone_aware to toTimestamp()

Bug: T325868
Change-Id: I5f77a51553fe11e9bfc93c63c361f90c5b123fc8
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 9202523..9b78121 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -578,17 +578,23 @@
return self.FORMATSTR.format(self.year, self.month, self.day,
self.hour, self.minute, self.second)

- def toTimestamp(self) -> Timestamp:
+ def toTimestamp(self, timezone_aware: bool = False) -> Timestamp:
"""
Convert the data to a pywikibot.Timestamp.

+ :param timezone_aware: Whether the timezone should be passed to
+ the Timestamp object.
:raises ValueError: instance value cannot be represented using
Timestamp
"""
if self.year <= 0:
raise ValueError('You cannot turn BC dates into a Timestamp')
- return Timestamp.fromISOformat(
+ ts = Timestamp.fromISOformat(
self.toTimestr(force_iso=True).lstrip('+'))
+ if timezone_aware:
+ ts = ts.replace(tzinfo=datetime.timezone(
+ datetime.timedelta(minutes=self.timezone)))
+ return ts

def toWikibase(self) -> Dict[str, Any]:
"""
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 3867219..e639277 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -6,6 +6,7 @@
# Distributed under the terms of the MIT license.
#
import copy
+import datetime
import json
import unittest
from contextlib import suppress
@@ -369,6 +370,15 @@
self.assertEqual(
t, pywikibot.WbTime.fromTimestamp(timestamp, site=repo))

+ ts1 = pywikibot.Timestamp(
+ year=2022, month=12, day=21, hour=13,
+ tzinfo=datetime.timezone(datetime.timedelta(hours=-5)))
+ t1 = pywikibot.WbTime.fromTimestamp(ts1, timezone=-300, site=repo)
+ self.assertIsNotNone(t1.toTimestamp(timezone_aware=True).tzinfo)
+ self.assertIsNone(t1.toTimestamp(timezone_aware=False).tzinfo)
+ self.assertEqual(t1.toTimestamp(timezone_aware=True), ts1)
+ self.assertNotEqual(t1.toTimestamp(timezone_aware=False), ts1)
+
def test_WbTime_errors(self):
"""Test WbTime precision errors."""
repo = self.get_repo()

To view, visit change 871019. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I5f77a51553fe11e9bfc93c63c361f90c5b123fc8
Gerrit-Change-Number: 871019
Gerrit-PatchSet: 6
Gerrit-Owner: RPI2026F1 <sarkaraoyan+rpi2026f1@gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mpaa <mpaa.wiki@gmail.com>
Gerrit-MessageType: merged