Mpaa has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/381979
)
Change subject: [bugfix] re-enable sep parameter for Timestamp.isoformat
......................................................................
[bugfix] re-enable sep parameter for Timestamp.isoformat
- Timestamp.isoformat was redefined but does not support "sep" parameter
anymore which is an optional parameter of datetime.isoformat() objects
which is the superclass of Timestamp. Re-enable this parameter to be
compatible with the datetime object again (and with compat btw.)
- Also allow a "sep" parameter in fromISOformat method to convert it back
- Tests added
Bug: T177285
Change-Id: Ie5209267188173c5702ba11378d55e25e9f8a193
---
M pywikibot/__init__.py
M tests/timestamp_tests.py
2 files changed, 59 insertions(+), 7 deletions(-)
Approvals:
Mpaa: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 348b596..6404b19 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -68,6 +68,7 @@
from pywikibot.site import BaseSite
from pywikibot.tools import (
# __ to avoid conflict with ModuleDeprecationWrapper._deprecated
+ classproperty,
deprecated as __deprecated,
deprecate_arg as _deprecate_arg,
normalize_username,
@@ -156,21 +157,45 @@
"""
mediawikiTSFormat = "%Y%m%d%H%M%S"
- ISO8601Format = "%Y-%m-%dT%H:%M:%SZ"
_ISO8601Format_new = '{0:+05d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z'
def clone(self):
"""Clone this instance."""
return self.replace(microsecond=self.microsecond)
+ @classproperty
+ def ISO8601Format(cls): # flake8: disable=N805
+ """ISO8601 format string class property for compatibility
purpose."""
+ return cls._ISO8601Format()
+
@classmethod
- def fromISOformat(cls, ts):
- """Convert an ISO 8601 timestamp to a Timestamp
object."""
+ def _ISO8601Format(cls, sep='T'):
+ """ISO8601 format string.
+
+ @param sep: one-character separator, placed between the date and time
+ @type sep: str
+ @return: ISO8601 format string
+ @rtype: str
+ """
+ assert(len(sep) == 1)
+ return '%Y-%m-%d{0}%H:%M:%SZ'.format(sep)
+
+ @classmethod
+ def fromISOformat(cls, ts, sep='T'):
+ """Convert an ISO 8601 timestamp to a Timestamp object.
+
+ @param ts: ISO 8601 timestamp or a Timestamp object already
+ @type ts: str ot Timestamp
+ @param sep: one-character separator, placed between the date and time
+ @type sep: str
+ @return: Timestamp object
+ @rtype: Timestamp
+ """
# If inadvertantly passed a Timestamp object, use replace()
# to create a clone.
if isinstance(ts, cls):
return ts.clone()
- return cls.strptime(ts, cls.ISO8601Format)
+ return cls.strptime(ts, cls._ISO8601Format(sep))
@classmethod
def fromtimestampformat(cls, ts):
@@ -181,7 +206,7 @@
return ts.clone()
return cls.strptime(ts, cls.mediawikiTSFormat)
- def isoformat(self):
+ def isoformat(self, sep='T'):
"""
Convert object to an ISO 8601 timestamp accepted by MediaWiki.
@@ -189,7 +214,7 @@
with a 'Z' unless a timezone is included, which causes MediaWiki
~1.19 and earlier to fail.
"""
- return self.strftime(self.ISO8601Format)
+ return self.strftime(self._ISO8601Format(sep))
toISOformat = redirect_func(isoformat, old_name='toISOformat',
class_name='Timestamp')
diff --git a/tests/timestamp_tests.py b/tests/timestamp_tests.py
index 2431164..a466352 100644
--- a/tests/timestamp_tests.py
+++ b/tests/timestamp_tests.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Tests for the Timestamp class."""
#
-# (C) Pywikibot team, 2014-2016
+# (C) Pywikibot team, 2014-2017
#
# Distributed under the terms of the MIT license.
#
@@ -9,6 +9,7 @@
import calendar
import datetime
+import re
from pywikibot import Timestamp
@@ -40,6 +41,7 @@
def test_iso_format(self):
"""Test conversion from and to ISO format."""
+ SEP = 'T'
t1 = Timestamp.utcnow()
ts1 = t1.isoformat()
t2 = Timestamp.fromISOformat(ts1)
@@ -49,6 +51,31 @@
t1 = t1.replace(microsecond=0)
self.assertEqual(t1, t2)
self.assertEqual(ts1, ts2)
+ date, sep, time = ts1.partition(SEP)
+ time = time.rstrip('Z')
+ self.assertEqual(date, str(t1.date()))
+ self.assertEqual(time, str(t1.time()))
+
+ def test_iso_format_with_sep(self):
+ """Test conversion from and to ISO format with
separator."""
+ SEP = '*'
+ t1 = Timestamp.utcnow().replace(microsecond=0)
+ ts1 = t1.isoformat(sep=SEP)
+ t2 = Timestamp.fromISOformat(ts1, sep=SEP)
+ ts2 = t2.isoformat(sep=SEP)
+ self.assertEqual(t1, t2)
+ self.assertEqual(t1, t2)
+ self.assertEqual(ts1, ts2)
+ date, sep, time = ts1.partition(SEP)
+ time = time.rstrip('Z')
+ self.assertEqual(date, str(t1.date()))
+ self.assertEqual(time, str(t1.time()))
+
+ def test_iso_format_property(self):
+ """Test iso format properties."""
+ self.assertEqual(Timestamp.ISO8601Format, Timestamp._ISO8601Format())
+ self.assertEqual(re.sub('[\-\:TZ]', '',
Timestamp.ISO8601Format),
+ Timestamp.mediawikiTSFormat)
def test_mediawiki_format(self):
"""Test conversion from and to timestamp
format."""
--
To view, visit
https://gerrit.wikimedia.org/r/381979
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie5209267188173c5702ba11378d55e25e9f8a193
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Legoktm <legoktm(a)member.fsf.org>
Gerrit-Reviewer: Lokal Profil <lokal.profil(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>