Skip to content

Commit c72d219

Browse files
Apply Python changes and update docs
1 parent f7c3f59 commit c72d219

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

Doc/library/datetime.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ Instance methods:
16061606

16071607
.. versionadded:: 3.6
16081608
Added the *timespec* argument.
1609-
.. versionadded:: 3.11
1609+
.. versionadded:: next
16101610
Added the *use_utc_designator* argument.
16111611

16121612
.. method:: datetime.__str__()
@@ -2003,15 +2003,12 @@ Instance methods:
20032003
>>> dt.isoformat(timespec='auto')
20042004
'12:34:56'
20052005
>>> dt = time(12, 30, 59, tzinfo=timezone.utc)
2006-
>>> dt.isoformat()
2007-
'12:30:59+00:00'
20082006
>>> dt.isoformat(use_utc_designator=True)
20092007
'12:30:59Z'
20102008

20112009
.. versionchanged:: 3.6
20122010
Added the *timespec* parameter.
2013-
2014-
.. versionadded:: 3.11
2011+
.. versionadded:: next
20152012
Added the *use_utc_designator* argument.
20162013

20172014

Lib/_pydatetime.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ def __repr__(self):
15981598
s = s[:-1] + ", fold=1)"
15991599
return s
16001600

1601-
def isoformat(self, timespec='auto'):
1601+
def isoformat(self, timespec='auto', use_utc_designator=False):
16021602
"""Return the time formatted according to ISO.
16031603
16041604
The full format is 'HH:MM:SS.mmmmmm+zz:zz'. By default, the fractional
@@ -1607,12 +1607,19 @@ def isoformat(self, timespec='auto'):
16071607
The optional argument timespec specifies the number of additional
16081608
terms of the time to include. Valid options are 'auto', 'hours',
16091609
'minutes', 'seconds', 'milliseconds' and 'microseconds'.
1610+
1611+
The UTC offset will be replaced with 'Z' if use_utc_designator
1612+
is True and self.tzname() is exactly 'UTC'.
16101613
"""
16111614
s = _format_time(self._hour, self._minute, self._second,
16121615
self._microsecond, timespec)
1613-
tz = self._tzstr()
1614-
if tz:
1615-
s += tz
1616+
1617+
if use_utc_designator and self.tzname() == 'UTC':
1618+
s += 'Z'
1619+
else:
1620+
tz = self._tzstr()
1621+
if tz:
1622+
s += tz
16161623
return s
16171624

16181625
__str__ = isoformat
@@ -2128,7 +2135,7 @@ def ctime(self):
21282135
self._hour, self._minute, self._second,
21292136
self._year)
21302137

2131-
def isoformat(self, sep='T', timespec='auto'):
2138+
def isoformat(self, sep='T', timespec='auto', use_utc_designator=False):
21322139
"""Return the time formatted according to ISO.
21332140
21342141
The full format looks like 'YYYY-MM-DD HH:MM:SS.mmmmmm'.
@@ -2143,15 +2150,21 @@ def isoformat(self, sep='T', timespec='auto'):
21432150
The optional argument timespec specifies the number of additional
21442151
terms of the time to include. Valid options are 'auto', 'hours',
21452152
'minutes', 'seconds', 'milliseconds' and 'microseconds'.
2153+
2154+
The UTC offset will be replaced with 'Z' if use_utc_designator
2155+
is True and self.tzname() is exactly 'UTC'.
21462156
"""
21472157
s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day, sep) +
21482158
_format_time(self._hour, self._minute, self._second,
21492159
self._microsecond, timespec))
21502160

21512161
off = self.utcoffset()
2152-
tz = _format_offset(off)
2153-
if tz:
2154-
s += tz
2162+
if use_utc_designator and (self.tzinfo is timezone.utc or self.tzname() == 'UTC'):
2163+
s += 'Z'
2164+
else:
2165+
tz = _format_offset(off)
2166+
if tz:
2167+
s += tz
21552168

21562169
return s
21572170

0 commit comments

Comments
 (0)