MCPcopy
hub / github.com/django/django / sanitize_strftime_format

Function sanitize_strftime_format

django/utils/formats.py:244–273  ·  view source on GitHub ↗

Ensure that certain specifiers are correctly padded with leading zeros. For years < 1000 specifiers %C, %F, %G, and %Y don't work as expected for strftime provided by glibc on Linux as they don't pad the year or century with leading zeros. Support for specifying the padding explici

(fmt)

Source from the content-addressed store, hash-verified

242
243@functools.lru_cache
244def sanitize_strftime_format(fmt):
245 """
246 Ensure that certain specifiers are correctly padded with leading zeros.
247
248 For years < 1000 specifiers %C, %F, %G, and %Y don&#x27;t work as expected for
249 strftime provided by glibc on Linux as they don&#x27;t pad the year or century
250 with leading zeros. Support for specifying the padding explicitly is
251 available, however, which can be used to fix this issue.
252
253 FreeBSD, macOS, and Windows do not support explicitly specifying the
254 padding, but return four digit years (with leading zeros) as expected.
255
256 This function checks whether the %Y produces a correctly padded string and,
257 if not, makes the following substitutions:
258
259 - %C → %02C
260 - %F → %010F
261 - %G → %04G
262 - %Y → %04Y
263
264 See https://bugs.python.org/issue13305 for more details.
265 """
266 if datetime.date(1, 1, 1).strftime("%Y") == "0001":
267 return fmt
268 mapping = {"C": 2, "F": 10, "G": 4, "Y": 4}
269 return re.sub(
270 r"((?:^|[^%])(?:%%)*)%([CFGY])",
271 lambda m: r"%s%%0%s%s" % (m[1], mapping[m[2]], m[2]),
272 fmt,
273 )
274
275
276def sanitize_separators(value):

Calls

no outgoing calls