MCPcopy Index your code
hub / github.com/RustPython/RustPython / _strptime

Function _strptime

Lib/_strptime.py:516–778  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

514
515
516def _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:

Callers 4

_strptime_timeFunction · 0.85
_strptime_datetime_dateFunction · 0.85
_strptime_datetime_timeFunction · 0.85

Calls 15

compileMethod · 0.95
enumerateFunction · 0.85
isinstanceFunction · 0.85
_getlangFunction · 0.85
TimeREClass · 0.85
lenFunction · 0.85
parse_intFunction · 0.85
_calc_julian_from_U_or_WFunction · 0.85
groupdictMethod · 0.80
fromisocalendarMethod · 0.80
toordinalMethod · 0.80
fromordinalMethod · 0.80

Tested by

no test coverage detected