@@ -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