Checks validity of parsed elements. :param parsed: The elements to parse. :return: The validated elements.
(
self, parsed: dict[str, Any], now: pendulum.DateTime
)
| 416 | return self._check_parsed(parsed, now) |
| 417 | |
| 418 | def _check_parsed( |
| 419 | self, parsed: dict[str, Any], now: pendulum.DateTime |
| 420 | ) -> dict[str, Any]: |
| 421 | """ |
| 422 | Checks validity of parsed elements. |
| 423 | |
| 424 | :param parsed: The elements to parse. |
| 425 | |
| 426 | :return: The validated elements. |
| 427 | """ |
| 428 | validated: dict[str, int | Timezone | None] = { |
| 429 | "year": parsed["year"], |
| 430 | "month": parsed["month"], |
| 431 | "day": parsed["day"], |
| 432 | "hour": parsed["hour"], |
| 433 | "minute": parsed["minute"], |
| 434 | "second": parsed["second"], |
| 435 | "microsecond": parsed["microsecond"], |
| 436 | "tz": None, |
| 437 | } |
| 438 | |
| 439 | # If timestamp has been specified |
| 440 | # we use it and don't go any further |
| 441 | if parsed["timestamp"] is not None: |
| 442 | str_us = str(parsed["timestamp"]) |
| 443 | if "." in str_us: |
| 444 | microseconds = int(f"{str_us.split('.')[1].ljust(6, '0')}") |
| 445 | else: |
| 446 | microseconds = 0 |
| 447 | |
| 448 | from pendulum.helpers import local_time |
| 449 | |
| 450 | time = local_time(parsed["timestamp"], 0, microseconds) |
| 451 | validated["year"] = time[0] |
| 452 | validated["month"] = time[1] |
| 453 | validated["day"] = time[2] |
| 454 | validated["hour"] = time[3] |
| 455 | validated["minute"] = time[4] |
| 456 | validated["second"] = time[5] |
| 457 | validated["microsecond"] = time[6] |
| 458 | |
| 459 | return validated |
| 460 | |
| 461 | if parsed["quarter"] is not None: |
| 462 | if validated["year"] is not None: |
| 463 | dt = pendulum.datetime(cast("int", validated["year"]), 1, 1) |
| 464 | else: |
| 465 | dt = now |
| 466 | |
| 467 | dt = dt.start_of("year") |
| 468 | |
| 469 | while dt.quarter != parsed["quarter"]: |
| 470 | dt = dt.add(months=3) |
| 471 | |
| 472 | validated["year"] = dt.year |
| 473 | validated["month"] = dt.month |
| 474 | validated["day"] = dt.day |
| 475 |