ISO 8601 compliant parser. :param text: The string to parse :type text: str :rtype: datetime.datetime or datetime.time or datetime.date
(
text: str,
)
| 83 | |
| 84 | |
| 85 | def parse_iso8601( |
| 86 | text: str, |
| 87 | ) -> datetime.datetime | datetime.date | datetime.time | Duration: |
| 88 | """ |
| 89 | ISO 8601 compliant parser. |
| 90 | |
| 91 | :param text: The string to parse |
| 92 | :type text: str |
| 93 | |
| 94 | :rtype: datetime.datetime or datetime.time or datetime.date |
| 95 | """ |
| 96 | parsed = _parse_iso8601_duration(text) |
| 97 | if parsed is not None: |
| 98 | return parsed |
| 99 | |
| 100 | m = ISO8601_DT.fullmatch(text) |
| 101 | if not m: |
| 102 | raise ParserError("Invalid ISO 8601 string") |
| 103 | |
| 104 | ambiguous_date = False |
| 105 | is_date = False |
| 106 | is_time = False |
| 107 | year = 0 |
| 108 | month = 1 |
| 109 | day = 1 |
| 110 | minute = 0 |
| 111 | second = 0 |
| 112 | microsecond = 0 |
| 113 | tzinfo: FixedTimezone | Timezone | None = None |
| 114 | |
| 115 | if m.group("date"): |
| 116 | # A date has been specified |
| 117 | is_date = True |
| 118 | |
| 119 | if m.group("isocalendar"): |
| 120 | # We have a ISO 8601 string defined |
| 121 | # by week number |
| 122 | if ( |
| 123 | m.group("weeksep") |
| 124 | and not m.group("weekdaysep") |
| 125 | and m.group("isoweekday") |
| 126 | ): |
| 127 | raise ParserError(f"Invalid date string: {text}") |
| 128 | |
| 129 | if not m.group("weeksep") and m.group("weekdaysep"): |
| 130 | raise ParserError(f"Invalid date string: {text}") |
| 131 | |
| 132 | try: |
| 133 | date = _get_iso_8601_week( |
| 134 | m.group("isoyear"), m.group("isoweek"), m.group("isoweekday") |
| 135 | ) |
| 136 | except ParserError: |
| 137 | raise |
| 138 | except ValueError: |
| 139 | raise ParserError(f"Invalid date string: {text}") |
| 140 | |
| 141 | year = date["year"] |
| 142 | month = date["month"] |
searching dependent graphs…