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