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)
| 242 | |
| 243 | @functools.lru_cache |
| 244 | def 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't work as expected for |
| 249 | strftime provided by glibc on Linux as they don'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 | |
| 276 | def sanitize_separators(value): |
no outgoing calls