8000 xmlrpc.client uses datetime.datetime.isoformat() (#105741) · python/cpython@307bcea · GitHub
[go: up one dir, main page]

Skip to content

Commit 307bcea

Browse files
vstinnerpganssle
andauthored
xmlrpc.client uses datetime.datetime.isoformat() (#105741)
Reimplement _iso8601_format() using the datetime isoformat() method. Ignore the timezone. Co-Authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
1 parent 7b1f0f2 commit 307bcea

File tree

2 files changed

+12
-32
lines changed

2 files changed

+12
-32
lines changed

Lib/test/test_xmlrpc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,16 @@ def test_time_struct(self):
504504
self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d))
505505

506506
def test_datetime_datetime(self):
507+
# naive (no tzinfo)
507508
d = datetime.datetime(2007,1,2,3,4,5)
508509
t = xmlrpclib.DateTime(d)
509510
self.assertEqual(str(t), '20070102T03:04:05')
510511

512+
# aware (with tzinfo): the timezone is ignored
513+
d = datetime.datetime(2023, 6, 12, 13, 30, tzinfo=datetime.UTC)
514+
t = xmlrpclib.DateTime(d)
515+
self.assertEqual(str(t), '20230612T13:30:00')
516+
511517
def test_repr(self):
512518
d = datetime.datetime(2007,1,2,3,4,5)
513519
t = xmlrpclib.DateTime(d)

Lib/xmlrpc/client.py

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -245,41 +245,15 @@ def __repr__(self):
245245

246246
##
247247
# Backwards compatibility
248-
249248
boolean = Boolean = bool
250249

251-
##
252-
# Wrapper for XML-RPC DateTime values. This converts a time value to
253-
# the format used by XML-RPC.
254-
# <p>
255-
# The value can be given as a datetime object, as a string in the
256-
# format "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by
257-
# time.localtime()), or an integer value (as returned by time.time()).
258-
# The wrapper uses time.localtime() to convert an integer to a time
259-
# tuple.
260-
#
261-
# @param value The time, given as a datetime object, an ISO 8601 string,
262-
# a time tuple, or an integer time value.
263-
264250

265-
# Issue #13305: different format codes across platforms
266-
_day0 = datetime(1, 1, 1)
267-
def _try(fmt):
268-
try:
269-
return _day0.strftime(fmt) == '0001'
270-
except ValueError:
271-
return False
272-
if _try('%Y'): # Mac OS X
273-
def _iso8601_format(value):
274-
return value.strftime("%Y%m%dT%H:%M:%S")
275-
elif _try('%4Y'): # Linux
276-
def _iso8601_format(value):
277-
return value.strftime("%4Y%m%dT%H:%M:%S")
278-
else:
279-
def _iso8601_format(value):
280-
return value.strftime("%Y%m%dT%H:%M:%S").zfill(17)
281-
del _day0
282-
del _try
251+
def _iso8601_format(value):
252+
if value.tzinfo is not None:
253+
# XML-RPC only uses the naive portion of the datetime
254+
value = value.replace(tzinfo=None)
255+
# XML-RPC doesn't use '-' separator in the date part
256+
return value.isoformat(timespec='seconds').replace('-', '')
283257

284258

285259
def _strftime(value):

0 commit comments

Comments
 (0)
0