Skip to content

Commit 8f29d9d

Browse files
add %:z strftime format code (pure python implementation)
1 parent b22f05a commit 8f29d9d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Lib/datetime.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ def _wrap_strftime(object, format, timetuple):
202202
# Don't call utcoffset() or tzname() unless actually needed.
203203
freplace = None # the string to use for %f
204204
zreplace = None # the string to use for %z
205+
colonzreplace = None # the string to use for %:z
205206
Zreplace = None # the string to use for %Z
206207

207-
# Scan format for %z and %Z escapes, replacing as needed.
208+
# Scan format for %z, %:z and %Z escapes, replacing as needed.
208209
newformat = []
209210
push = newformat.append
210211
i, n = 0, len(format)
@@ -228,6 +229,22 @@ def _wrap_strftime(object, format, timetuple):
228229
zreplace = ""
229230
assert '%' not in zreplace
230231
newformat.append(zreplace)
232+
elif ch == ':':
233+
if i < n:
234+
ch2 = format[i]
235+
i += 1
236+
if ch2 == 'z':
237+
if colonzreplace is None:
238+
if hasattr(object, "utcoffset"):
239+
colonzreplace = _format_offset(object.utcoffset(), sep=":")
240+
else:
241+
colonzreplace = ""
242+
assert '%' not in colonzreplace
243+
newformat.append(colonzreplace)
244+
else:
245+
push('%')
246+
push(ch)
247+
push(ch2)
231248
elif ch == 'Z':
232249
if Zreplace is None:
233250
Zreplace = ""

0 commit comments

Comments
 (0)