Parses a string with the given options. :param text: The string to parse.
(
text: str, **options: t.Any
)
| 37 | |
| 38 | |
| 39 | def _parse( |
| 40 | text: str, **options: t.Any |
| 41 | ) -> Date | DateTime | Time | Duration | Interval[DateTime]: |
| 42 | """ |
| 43 | Parses a string with the given options. |
| 44 | |
| 45 | :param text: The string to parse. |
| 46 | """ |
| 47 | # Handling special cases |
| 48 | if text == "now": |
| 49 | return pendulum.now(tz=options.get("tz", UTC)) |
| 50 | |
| 51 | parsed = base_parse(text, **options) |
| 52 | |
| 53 | if isinstance(parsed, datetime.datetime): |
| 54 | return pendulum.datetime( |
| 55 | parsed.year, |
| 56 | parsed.month, |
| 57 | parsed.day, |
| 58 | parsed.hour, |
| 59 | parsed.minute, |
| 60 | parsed.second, |
| 61 | parsed.microsecond, |
| 62 | tz=parsed.tzinfo or options.get("tz", UTC), |
| 63 | ) |
| 64 | |
| 65 | if isinstance(parsed, datetime.date): |
| 66 | return pendulum.date(parsed.year, parsed.month, parsed.day) |
| 67 | |
| 68 | if isinstance(parsed, datetime.time): |
| 69 | return pendulum.time( |
| 70 | parsed.hour, parsed.minute, parsed.second, parsed.microsecond |
| 71 | ) |
| 72 | |
| 73 | if isinstance(parsed, _Interval): |
| 74 | if parsed.duration is not None: |
| 75 | duration = parsed.duration |
| 76 | |
| 77 | if parsed.start is not None: |
| 78 | dt = pendulum.instance(parsed.start, tz=options.get("tz", UTC)) |
| 79 | |
| 80 | return pendulum.interval( |
| 81 | dt, |
| 82 | dt.add( |
| 83 | years=duration.years, |
| 84 | months=duration.months, |
| 85 | weeks=duration.weeks, |
| 86 | days=duration.remaining_days, |
| 87 | hours=duration.hours, |
| 88 | minutes=duration.minutes, |
| 89 | seconds=duration.remaining_seconds, |
| 90 | microseconds=duration.microseconds, |
| 91 | ), |
| 92 | ) |
| 93 | |
| 94 | dt = pendulum.instance( |
| 95 | t.cast("datetime.datetime", parsed.end), tz=options.get("tz", UTC) |
| 96 | ) |
no test coverage detected
searching dependent graphs…