Return a 2-tuple consisting of a time struct and an int containing the number of microseconds based on the input string and the format string.
(data_string, format="%a %b %d %H:%M:%S %Y")
| 514 | |
| 515 | |
| 516 | def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"): |
| 517 | """Return a 2-tuple consisting of a time struct and an int containing |
| 518 | the number of microseconds based on the input string and the |
| 519 | format string.""" |
| 520 | |
| 521 | for index, arg in enumerate([data_string, format]): |
| 522 | if not isinstance(arg, str): |
| 523 | msg = "strptime() argument {} must be str, not {}" |
| 524 | raise TypeError(msg.format(index, type(arg))) |
| 525 | |
| 526 | global _TimeRE_cache, _regex_cache |
| 527 | with _cache_lock: |
| 528 | locale_time = _TimeRE_cache.locale_time |
| 529 | if (_getlang() != locale_time.lang or |
| 530 | time.tzname != locale_time.tzname or |
| 531 | time.daylight != locale_time.daylight): |
| 532 | _TimeRE_cache = TimeRE() |
| 533 | _regex_cache.clear() |
| 534 | locale_time = _TimeRE_cache.locale_time |
| 535 | if len(_regex_cache) > _CACHE_MAX_SIZE: |
| 536 | _regex_cache.clear() |
| 537 | format_regex = _regex_cache.get(format) |
| 538 | if not format_regex: |
| 539 | try: |
| 540 | format_regex = _TimeRE_cache.compile(format) |
| 541 | # KeyError raised when a bad format is found; can be specified as |
| 542 | # \\, in which case it was a stray % but with a space after it |
| 543 | except KeyError as err: |
| 544 | bad_directive = err.args[0] |
| 545 | del err |
| 546 | bad_directive = bad_directive.replace('\\s', '') |
| 547 | if not bad_directive: |
| 548 | raise ValueError("stray %% in format '%s'" % format) from None |
| 549 | bad_directive = bad_directive.replace('\\', '', 1) |
| 550 | raise ValueError("'%s' is a bad directive in format '%s'" % |
| 551 | (bad_directive, format)) from None |
| 552 | _regex_cache[format] = format_regex |
| 553 | found = format_regex.match(data_string) |
| 554 | if not found: |
| 555 | raise ValueError("time data %r does not match format %r" % |
| 556 | (data_string, format)) |
| 557 | if len(data_string) != found.end(): |
| 558 | raise ValueError("unconverted data remains: %s" % |
| 559 | data_string[found.end():]) |
| 560 | |
| 561 | iso_year = year = None |
| 562 | month = day = 1 |
| 563 | hour = minute = second = fraction = 0 |
| 564 | tz = -1 |
| 565 | gmtoff = None |
| 566 | gmtoff_fraction = 0 |
| 567 | iso_week = week_of_year = None |
| 568 | week_of_year_start = None |
| 569 | # weekday and julian defaulted to None so as to signal need to calculate |
| 570 | # values |
| 571 | weekday = julian = None |
| 572 | found_dict = found.groupdict() |
| 573 | if locale_time.LC_alt_digits: |
no test coverage detected