]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
xmlrpc.client uses datetime.datetime.isoformat() (#105741)
authorVictor Stinner <vstinner@python.org>
Wed, 14 Jun 2023 15:00:40 +0000 (17:00 +0200)
committerGitHub <noreply@github.com>
Wed, 14 Jun 2023 15:00:40 +0000 (17:00 +0200)
Reimplement _iso8601_format() using the datetime isoformat() method.
Ignore the timezone.

Co-Authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
Lib/test/test_xmlrpc.py
Lib/xmlrpc/client.py

index 9ff5545f786a3207899484f33c16027404860a53..edc741dbc60088b93d4dcf5f76508ce29d37c965 100644 (file)
@@ -504,10 +504,16 @@ class DateTimeTestCase(unittest.TestCase):
         self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d))
 
     def test_datetime_datetime(self):
+        # naive (no tzinfo)
         d = datetime.datetime(2007,1,2,3,4,5)
         t = xmlrpclib.DateTime(d)
         self.assertEqual(str(t), '20070102T03:04:05')
 
+        # aware (with tzinfo): the timezone is ignored
+        d = datetime.datetime(2023, 6, 12, 13, 30, tzinfo=datetime.UTC)
+        t = xmlrpclib.DateTime(d)
+        self.assertEqual(str(t), '20230612T13:30:00')
+
     def test_repr(self):
         d = datetime.datetime(2007,1,2,3,4,5)
         t = xmlrpclib.DateTime(d)
index ea8da766cb5a7e00559c3b8392402acaf815184b..f441376d09c4aa2ea88aef83fcd14ecc9e8db82f 100644 (file)
@@ -245,41 +245,15 @@ class Fault(Error):
 
 ##
 # Backwards compatibility
-
 boolean = Boolean = bool
 
-##
-# Wrapper for XML-RPC DateTime values.  This converts a time value to
-# the format used by XML-RPC.
-# <p>
-# The value can be given as a datetime object, as a string in the
-# format "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by
-# time.localtime()), or an integer value (as returned by time.time()).
-# The wrapper uses time.localtime() to convert an integer to a time
-# tuple.
-#
-# @param value The time, given as a datetime object, an ISO 8601 string,
-#              a time tuple, or an integer time value.
-
 
-# Issue #13305: different format codes across platforms
-_day0 = datetime(1, 1, 1)
-def _try(fmt):
-    try:
-        return _day0.strftime(fmt) == '0001'
-    except ValueError:
-        return False
-if _try('%Y'):      # Mac OS X
-    def _iso8601_format(value):
-        return value.strftime("%Y%m%dT%H:%M:%S")
-elif _try('%4Y'):   # Linux
-    def _iso8601_format(value):
-        return value.strftime("%4Y%m%dT%H:%M:%S")
-else:
-    def _iso8601_format(value):
-        return value.strftime("%Y%m%dT%H:%M:%S").zfill(17)
-del _day0
-del _try
+def _iso8601_format(value):
+    if value.tzinfo is not None:
+        # XML-RPC only uses the naive portion of the datetime
+        value = value.replace(tzinfo=None)
+    # XML-RPC doesn't use '-' separator in the date part
+    return value.isoformat(timespec='seconds').replace('-', '')
 
 
 def _strftime(value):