jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/480099 )
Change subject: [IMPR] Make Timestamp.fromtimestampformat() more robust
......................................................................
[IMPR] Make Timestamp.fromtimestampformat() more robust
- Also convert mediawiki timestamp to Timestamp if
year, month and day are given only
- Tests added
- Update welcome.py accordingly
Change-Id: I1e3e88d88a971db4d9d73bee6d245f046d78427c
---
M pywikibot/__init__.py
M scripts/welcome.py
M tests/timestamp_tests.py
3 files changed, 20 insertions(+), 7 deletions(-)
Approvals:
Zhuyifei1999: Looks good to me, but someone else must approve
Framawiki: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index fb07bcd..899710a 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -210,6 +210,8 @@
# to create a clone.
if isinstance(ts, cls):
return ts.clone()
+ if len(ts) == 8: # year, month and day are given only
+ ts += '000'
return cls.strptime(ts, cls.mediawikiTSFormat)
def isoformat(self, sep='T'):
diff --git a/scripts/welcome.py b/scripts/welcome.py
index 68bd0cc..d192cc1 100755
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -54,7 +54,7 @@
to give interactive users a chance to welcome the
new users (default: now)
Timezone is the server timezone, GMT for Wikimedia
- TIME format : yyyymmddhhmmss
+ TIME format : yyyymmddhhmmss or yyyymmdd
-timeoffset[:#] Skip the latest new users, accounts newer than
# minutes
@@ -172,6 +172,7 @@
import locale
import re
import sys
+from textwrap import fill
import time
from random import choice
@@ -935,16 +936,17 @@
if not val:
val = pywikibot.input(
'Which time offset for new users would you like to use? '
- '(yyyymmddhhmmss)')
+ '(yyyymmddhhmmss or yyyymmdd)')
try:
globalvar.offset = pywikibot.Timestamp.fromtimestampformat(val)
except ValueError:
# upon request, we could check for software version here
- raise ValueError(
+ raise ValueError(fill(
'Mediawiki has changed, -offset:# is not supported '
- 'anymore, but -offset:TIMESTAMP is, assuming TIMESTAMP '
- 'is yyyymmddhhmmss. -timeoffset is now also supported. '
- 'Please read this script source header for documentation.')
+ 'anymore, but -offset:TIMESTAMP is, assuming TIMESTAMP is '
+ 'yyyymmddhhmmss or yyyymmdd. -timeoffset is now also '
+ 'supported. Please read this script source header for '
+ 'documentation.'))
elif arg == '-file':
globalvar.randomSign = True
globalvar.signFileName = val or pywikibot.input(
diff --git a/tests/timestamp_tests.py b/tests/timestamp_tests.py
index 022a975..9cd4ed2 100644
--- a/tests/timestamp_tests.py
+++ b/tests/timestamp_tests.py
@@ -80,7 +80,7 @@
Timestamp.mediawikiTSFormat)
def test_mediawiki_format(self):
- """Test conversion from and to timestamp
format."""
+ """Test conversion from and to Timestamp
format."""
t1 = Timestamp.utcnow()
if not t1.microsecond: # T191827: ensure microsecond is not 0
t1 = t1.replace(microsecond=1000)
@@ -93,6 +93,15 @@
self.assertEqual(t1, t2)
self.assertEqual(ts1, ts2)
+ def test_short_mediawiki_format(self):
+ """Test short mw timestamp conversion from and to Timestamp
format."""
+ t1 = Timestamp(2018, 12, 17)
+ t2 = Timestamp.fromtimestampformat('20181217') # short timestamp
+ ts1 = t1.totimestampformat()
+ ts2 = t2.totimestampformat()
+ self.assertEqual(t1, t2)
+ self.assertEqual(ts1, ts2)
+
def test_add_timedelta(self):
"""Test addin a timedelta to a Timestamp."""
t1 = Timestamp.utcnow()
--
To view, visit
https://gerrit.wikimedia.org/r/480099
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I1e3e88d88a971db4d9d73bee6d245f046d78427c
Gerrit-Change-Number: 480099
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: jenkins-bot (75)