@@ -165,14 +165,23 @@ def _build_struct_time(y, m, d, hh, mm, ss, dstflag):
165165 dnum = _days_before_month (y , m ) + d
166166 return _time .struct_time ((y , m , d , hh , mm , ss , wday , dnum , dstflag ))
167167
168- def _format_time (hh , mm , ss , us , timespec = 'auto' ):
169- specs = {
170- 'hours' : '{:02d}' ,
171- 'minutes' : '{:02d}:{:02d}' ,
172- 'seconds' : '{:02d}:{:02d}:{:02d}' ,
173- 'milliseconds' : '{:02d}:{:02d}:{:02d}.{:03d}' ,
174- 'microseconds' : '{:02d}:{:02d}:{:02d}.{:06d}'
175- }
168+ def _format_time (hh , mm , ss , us , timespec = 'auto' , basic = False ):
169+ if basic :
170+ specs = {
171+ 'hours' : '{:02d}' ,
172+ 'minutes' : '{:02d}{:02d}' ,
173+ 'seconds' : '{:02d}{:02d}{:02d}' ,
174+ 'milliseconds' : '{:02d}{:02d}{:02d}.{:03d}' ,
175+ 'microseconds' : '{:02d}{:02d}{:02d}.{:06d}'
176+ }
177+ else :
178+ specs = {
179+ 'hours' : '{:02d}' ,
180+ 'minutes' : '{:02d}:{:02d}' ,
181+ 'seconds' : '{:02d}:{:02d}:{:02d}' ,
182+ 'milliseconds' : '{:02d}:{:02d}:{:02d}.{:03d}' ,
183+ 'microseconds' : '{:02d}:{:02d}:{:02d}.{:06d}'
184+ }
176185
177186 if timespec == 'auto' :
178187 # Skip trailing microseconds when us==0.
@@ -1051,16 +1060,18 @@ def __format__(self, fmt):
10511060 return self .strftime (fmt )
10521061 return str (self )
10531062
1054- def isoformat (self ):
1063+ def isoformat (self , basic = False ):
10551064 """Return the date formatted according to ISO.
10561065
1057- This is 'YYYY-MM-DD'.
1066+ This is 'YYYY-MM-DD' or 'YYYYMMDD' if *basic* is true .
10581067
10591068 References:
10601069 - http://www.w3.org/TR/NOTE-datetime
10611070 - http://www.cl.cam.ac.uk/~mgk25/iso-time.html
10621071 """
1063- return "%04d-%02d-%02d" % (self ._year , self ._month , self ._day )
1072+ if basic :
1073+ return f"{ self ._year :04d} { self ._month :02d} { self ._day :02d} "
1074+ return f"{ self ._year :04d} -{ self ._month :02d} -{ self ._day :02d} "
10641075
10651076 __str__ = isoformat
10661077
@@ -1501,10 +1512,11 @@ def __hash__(self):
15011512
15021513 # Conversion to string
15031514
1504- def _tzstr (self ):
1515+ def _tzstr (self , basic ):
15051516 """Return formatted timezone offset (+xx:xx) or an empty string."""
15061517 off = self .utcoffset ()
1507- return _format_offset (off )
1518+ sep = '' if basic else ':'
1519+ return _format_offset (off , sep )
15081520
15091521 def __repr__ (self ):
15101522 """Convert to formal string, for repr()."""
@@ -1525,19 +1537,21 @@ def __repr__(self):
15251537 s = s [:- 1 ] + ", fold=1)"
15261538 return s
15271539
1528- def isoformat (self , timespec = 'auto' ):
1540+ def isoformat (self , timespec = 'auto' , basic = False ):
15291541 """Return the time formatted according to ISO.
15301542
15311543 The full format is 'HH:MM:SS.mmmmmm+zz:zz'. By default, the fractional
15321544 part is omitted if self.microsecond == 0.
15331545
1546+ If *basic* is true, separators ':' are removed from the output.
1547+
15341548 The optional argument timespec specifies the number of additional
15351549 terms of the time to include. Valid options are 'auto', 'hours',
15361550 'minutes', 'seconds', 'milliseconds' and 'microseconds'.
15371551 """
15381552 s = _format_time (self ._hour , self ._minute , self ._second ,
1539- self ._microsecond , timespec )
1540- tz = self ._tzstr ()
1553+ self ._microsecond , timespec , basic )
1554+ tz = self ._tzstr (basic )
15411555 if tz :
15421556 s += tz
15431557 return s
@@ -2028,6 +2042,12 @@ def astimezone(self, tz=None):
20282042
20292043 # Ways to produce a string.
20302044
2045+ def _tzstr (self , basic ):
2046+ """Return formatted timezone offset (+xx:xx) or an empty string."""
2047+ off = self .utcoffset ()
2048+ sep = '' if basic else ':'
2049+ return _format_offset (off , sep )
2050+
20312051 def ctime (self ):
20322052 "Return ctime() style string."
20332053 weekday = self .toordinal () % 7 or 7
@@ -2038,12 +2058,14 @@ def ctime(self):
20382058 self ._hour , self ._minute , self ._second ,
20392059 self ._year )
20402060
2041- def isoformat (self , sep = 'T' , timespec = 'auto' ):
2061+ def isoformat (self , sep = 'T' , timespec = 'auto' , basic = False ):
20422062 """Return the time formatted according to ISO.
20432063
20442064 The full format looks like 'YYYY-MM-DD HH:MM:SS.mmmmmm'.
20452065 By default, the fractional part is omitted if self.microsecond == 0.
20462066
2067+ If *basic* is true, separators ':' and '-' are removed from the output.
2068+
20472069 If self.tzinfo is not None, the UTC offset is also attached, giving
20482070 giving a full format of 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM'.
20492071
@@ -2054,12 +2076,12 @@ def isoformat(self, sep='T', timespec='auto'):
20542076 terms of the time to include. Valid options are 'auto', 'hours',
20552077 'minutes', 'seconds', 'milliseconds' and 'microseconds'.
20562078 """
2057- s = ("%04d-%02d-%02d%c" % (self ._year , self ._month , self ._day , sep ) +
2079+ fmt = "%04d%02d%02d%c" if basic else "%04d-%02d-%02d%c"
2080+ s = (fmt % (self ._year , self ._month , self ._day , sep ) +
20582081 _format_time (self ._hour , self ._minute , self ._second ,
2059- self ._microsecond , timespec ))
2082+ self ._microsecond , timespec , basic ))
20602083
2061- off = self .utcoffset ()
2062- tz = _format_offset (off )
2084+ tz = self ._tzstr (basic )
20632085 if tz :
20642086 s += tz
20652087
0 commit comments