Tries to parse the string as a common datetime format. :param text: The string to parse.
(text: str, **options: Any)
| 135 | |
| 136 | |
| 137 | def _parse_common(text: str, **options: Any) -> datetime | date | time: |
| 138 | """ |
| 139 | Tries to parse the string as a common datetime format. |
| 140 | |
| 141 | :param text: The string to parse. |
| 142 | """ |
| 143 | m = COMMON.fullmatch(text) |
| 144 | has_date = False |
| 145 | year = 0 |
| 146 | month = 1 |
| 147 | day = 1 |
| 148 | |
| 149 | if not m: |
| 150 | raise ParserError("Invalid datetime string") |
| 151 | |
| 152 | if m.group("date"): |
| 153 | # A date has been specified |
| 154 | has_date = True |
| 155 | |
| 156 | year = int(m.group("year")) |
| 157 | |
| 158 | if not m.group("monthday"): |
| 159 | # No month and day |
| 160 | month = 1 |
| 161 | day = 1 |
| 162 | else: |
| 163 | if options["day_first"]: |
| 164 | month = int(m.group("day")) |
| 165 | day = int(m.group("month")) |
| 166 | else: |
| 167 | month = int(m.group("month")) |
| 168 | day = int(m.group("day")) |
| 169 | |
| 170 | if not m.group("time"): |
| 171 | return date(year, month, day) |
| 172 | |
| 173 | # Grabbing hh:mm:ss |
| 174 | hour = int(m.group("hour")) |
| 175 | |
| 176 | minute = int(m.group("minute")) |
| 177 | |
| 178 | second = int(m.group("second")) if m.group("second") else 0 |
| 179 | |
| 180 | # Grabbing subseconds, if any |
| 181 | microsecond = 0 |
| 182 | if m.group("subsecondsection"): |
| 183 | # Limiting to 6 chars |
| 184 | subsecond = m.group("subsecond")[:6] |
| 185 | |
| 186 | microsecond = int(f"{subsecond:0<6}") |
| 187 | |
| 188 | if has_date: |
| 189 | return datetime(year, month, day, hour, minute, second, microsecond) |
| 190 | |
| 191 | return time(hour, minute, second, microsecond) |
| 192 | |
| 193 | |
| 194 | class _Interval: |
no test coverage detected
searching dependent graphs…