Return a 3-tuple consisting of a tuple with time components, an int containing the number of microseconds, and an int containing the microseconds part of the GMT offset, based on the input string and the format string.
(data_string, format="%a %b %d %H:%M:%S %Y")
| 521 | |
| 522 | |
| 523 | def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"): |
| 524 | """Return a 3-tuple consisting of a tuple with time components, |
| 525 | an int containing the number of microseconds, and an int |
| 526 | containing the microseconds part of the GMT offset, based on the |
| 527 | input string and the format string.""" |
| 528 | |
| 529 | for index, arg in enumerate([data_string, format]): |
| 530 | if not isinstance(arg, str): |
| 531 | msg = "strptime() argument {} must be str, not {}" |
| 532 | raise TypeError(msg.format(index, type(arg))) |
| 533 | |
| 534 | global _TimeRE_cache, _regex_cache |
| 535 | with _cache_lock: |
| 536 | locale_time = _TimeRE_cache.locale_time |
| 537 | if (_getlang() != locale_time.lang or |
| 538 | time.tzname != locale_time.tzname or |
| 539 | time.daylight != locale_time.daylight): |
| 540 | _TimeRE_cache = TimeRE() |
| 541 | _regex_cache.clear() |
| 542 | locale_time = _TimeRE_cache.locale_time |
| 543 | if len(_regex_cache) > _CACHE_MAX_SIZE: |
| 544 | _regex_cache.clear() |
| 545 | format_regex = _regex_cache.get(format) |
| 546 | if not format_regex: |
| 547 | try: |
| 548 | format_regex = _TimeRE_cache.compile(format) |
| 549 | # KeyError raised when a bad format is found; can be specified as |
| 550 | # \\, in which case it was a stray % but with a space after it |
| 551 | except KeyError as err: |
| 552 | bad_directive = err.args[0] |
| 553 | del err |
| 554 | bad_directive = bad_directive.replace('\\s', '') |
| 555 | if not bad_directive: |
| 556 | raise ValueError("stray %% in format '%s'" % format) from None |
| 557 | bad_directive = bad_directive.replace('\\', '', 1) |
| 558 | raise ValueError("'%s' is a bad directive in format '%s'" % |
| 559 | (bad_directive, format)) from None |
| 560 | _regex_cache[format] = format_regex |
| 561 | found = format_regex.match(data_string) |
| 562 | if not found: |
| 563 | raise ValueError("time data %r does not match format %r" % |
| 564 | (data_string, format)) |
| 565 | if len(data_string) != found.end(): |
| 566 | rest = data_string[found.end():] |
| 567 | # Specific check for '%:z' directive |
| 568 | if ( |
| 569 | "colon_z" in found.re.groupindex |
| 570 | and found.group("colon_z") is not None |
| 571 | and rest[0] != ":" |
| 572 | ): |
| 573 | raise ValueError( |
| 574 | f"Missing colon in %:z before '{rest}', got '{data_string}'" |
| 575 | ) |
| 576 | raise ValueError("unconverted data remains: %s" % rest) |
| 577 | |
| 578 | iso_year = year = None |
| 579 | month = day = 1 |
| 580 | hour = minute = second = fraction = 0 |
no test coverage detected
searching dependent graphs…