MCPcopy Index your code
hub / github.com/python-pendulum/pendulum / parse

Method parse

src/pendulum/formatting/formatter.py:361–416  ·  view source on GitHub ↗

Parses a time string matching a given format as a tuple. :param time: The timestring :param fmt: The format :param now: The datetime to use as "now" :param locale: The locale to use :return: The parsed elements

(
        self,
        time: str,
        fmt: str,
        now: pendulum.DateTime,
        locale: str | None = None,
    )

Source from the content-addressed store, hash-verified

359 return token
360
361 def parse(
362 self,
363 time: str,
364 fmt: str,
365 now: pendulum.DateTime,
366 locale: str | None = None,
367 ) -> dict[str, Any]:
368 """
369 Parses a time string matching a given format as a tuple.
370
371 :param time: The timestring
372 :param fmt: The format
373 :param now: The datetime to use as "now"
374 :param locale: The locale to use
375
376 :return: The parsed elements
377 """
378 escaped_fmt = re.escape(fmt)
379
380 if not self._FROM_FORMAT_RE.search(escaped_fmt):
381 raise ValueError("The given time string does not match the given format")
382
383 if not locale:
384 locale = pendulum.get_locale()
385
386 loaded_locale: Locale = Locale.load(locale)
387
388 parsed = {
389 "year": None,
390 "month": None,
391 "day": None,
392 "hour": None,
393 "minute": None,
394 "second": None,
395 "microsecond": None,
396 "tz": None,
397 "quarter": None,
398 "day_of_week": None,
399 "day_of_year": None,
400 "meridiem": None,
401 "timestamp": None,
402 }
403
404 pattern = self._FROM_FORMAT_RE.sub(
405 lambda m: self._replace_tokens(m.group(0), loaded_locale), escaped_fmt
406 )
407
408 if not re.fullmatch(pattern, time):
409 raise ValueError(f"String does not match format {fmt}")
410
411 def _get_parsed_values(m: Match[str]) -> Any:
412 return self._get_parsed_values(m, parsed, loaded_locale, now)
413
414 re.sub(pattern, _get_parsed_values, time)
415
416 return self._check_parsed(parsed, now)
417
418 def _check_parsed(

Callers 12

from_formatFunction · 0.80
_parseFunction · 0.80
_check_parsedMethod · 0.80
test_parseFunction · 0.80
test_parse_with_timezoneFunction · 0.80
test_parse_exactFunction · 0.80
test_parse_durationFunction · 0.80
test_parse_intervalFunction · 0.80
test_parse_nowFunction · 0.80
test_from_formatFunction · 0.80

Calls 3

_replace_tokensMethod · 0.95
_check_parsedMethod · 0.95
loadMethod · 0.80

Tested by 9

test_parseFunction · 0.64
test_parse_with_timezoneFunction · 0.64
test_parse_exactFunction · 0.64
test_parse_durationFunction · 0.64
test_parse_intervalFunction · 0.64
test_parse_nowFunction · 0.64
test_from_formatFunction · 0.64