Skip to content

Commit b22f05a

Browse files
cleanup: datetime.py: reduce code duplication, reuse _format_offset
1 parent 14f935c commit b22f05a

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

Lib/datetime.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _format_time(hh, mm, ss, us, timespec='auto'):
179179
else:
180180
return fmt.format(hh, mm, ss, us)
181181

182-
def _format_offset(off):
182+
def _format_offset(off, sep=':'):
183183
s = ''
184184
if off is not None:
185185
if off.days < 0:
@@ -189,9 +189,9 @@ def _format_offset(off):
189189
sign = "+"
190190
hh, mm = divmod(off, timedelta(hours=1))
191191
mm, ss = divmod(mm, timedelta(minutes=1))
192-
s += "%s%02d:%02d" % (sign, hh, mm)
192+
s += "%s%02d%s%02d" % (sign, hh, sep, mm)
193193
if ss or ss.microseconds:
194-
s += ":%02d" % ss.seconds
194+
s += "%s%02d" % (sep, ss.seconds)
195195

196196
if ss.microseconds:
197197
s += '.%06d' % ss.microseconds
@@ -222,24 +222,10 @@ def _wrap_strftime(object, format, timetuple):
222222
newformat.append(freplace)
223223
elif ch == 'z':
224224
if zreplace is None:
225-
zreplace = ""
226225
if hasattr(object, "utcoffset"):
227-
offset = object.utcoffset()
228-
if offset is not None:
229-
sign = '+'
230-
if offset.days < 0:
231-
offset = -offset
232-
sign = '-'
233-
h, rest = divmod(offset, timedelta(hours=1))
234-
m, rest = divmod(rest, timedelta(minutes=1))
235-
s = rest.seconds
236-
u = offset.microseconds
237-
if u:
238-
zreplace = '%c%02d%02d%02d.%06d' % (sign, h, m, s, u)
239-
elif s:
240-
zreplace = '%c%02d%02d%02d' % (sign, h, m, s)
241-
else:
242-
zreplace = '%c%02d%02d' % (sign, h, m)
226+
zreplace = _format_offset(object.utcoffset(), sep="")
227+
else:
228+
zreplace = ""
243229
assert '%' not in zreplace
244230
newformat.append(zreplace)
245231
elif ch == 'Z':

0 commit comments

Comments
 (0)