Return regex pattern for the format string. Need to make sure that any characters that might be interpreted as regex syntax are escaped.
(self, format)
| 444 | return '(?P<%s>%s)' % (directive, regex) |
| 445 | |
| 446 | def pattern(self, format): |
| 447 | """Return regex pattern for the format string. |
| 448 | |
| 449 | Need to make sure that any characters that might be interpreted as |
| 450 | regex syntax are escaped. |
| 451 | |
| 452 | """ |
| 453 | # The sub() call escapes all characters that might be misconstrued |
| 454 | # as regex syntax. Cannot use re.escape since we have to deal with |
| 455 | # format directives (%m, etc.). |
| 456 | format = re_sub(r"([\\.^$*+?\(\){}\[\]|])", r"\\\1", format) |
| 457 | format = re_sub(r'\s+', r'\\s+', format) |
| 458 | format = re_sub(r"'", "['\u02bc]", format) # needed for br_FR |
| 459 | year_in_format = False |
| 460 | day_of_month_in_format = False |
| 461 | def repl(m): |
| 462 | format_char = m[1] |
| 463 | match format_char: |
| 464 | case 'Y' | 'y' | 'G': |
| 465 | nonlocal year_in_format |
| 466 | year_in_format = True |
| 467 | case 'd': |
| 468 | nonlocal day_of_month_in_format |
| 469 | day_of_month_in_format = True |
| 470 | return self[format_char] |
| 471 | format = re_sub(r'%[-_0^#]*[0-9]*([OE]?\\?.?)', repl, format) |
| 472 | if day_of_month_in_format and not year_in_format: |
| 473 | import warnings |
| 474 | warnings.warn("""\ |
| 475 | Parsing dates involving a day of month without a year specified is ambiguous |
| 476 | and fails to parse leap day. The default behavior will change in Python 3.15 |
| 477 | to either always raise an exception or to use a different default year (TBD). |
| 478 | To avoid trouble, add a specific year to the input & format. |
| 479 | See https://github.com/python/cpython/issues/70647.""", |
| 480 | DeprecationWarning, |
| 481 | skip_file_prefixes=(os.path.dirname(__file__),)) |
| 482 | return format |
| 483 | |
| 484 | def compile(self, format): |
| 485 | """Return a compiled re object for the format string.""" |