MCPcopy Index your code
hub / github.com/python/cpython / _strptime

Function _strptime

Lib/_strptime.py:523–796  ·  view source on GitHub ↗

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")

Source from the content-addressed store, hash-verified

521
522
523def _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

Callers 4

_strptime_timeFunction · 0.85
_strptime_datetime_dateFunction · 0.85
_strptime_datetime_timeFunction · 0.85

Calls 15

compileMethod · 0.95
enumerateFunction · 0.85
_getlangFunction · 0.85
TimeREClass · 0.85
parse_intFunction · 0.85
_calc_julian_from_U_or_WFunction · 0.85
fromisocalendarMethod · 0.80
toordinalMethod · 0.80
fromordinalMethod · 0.80
formatMethod · 0.45
clearMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…