|
| 1 | +# pylint: disable=r0903 |
| 2 | +import datetime as dt |
| 3 | +import re |
| 4 | + |
| 5 | +from vobjectx.exceptions import ParseError |
| 6 | +from vobjectx.registry import TzidRegistry |
| 7 | + |
| 8 | + |
| 9 | +def _is_duration(s: str) -> bool: |
| 10 | + return "P" in s[:2].upper() |
| 11 | + |
| 12 | + |
| 13 | +class Date: |
| 14 | + def __init__(self, date_: str): |
| 15 | + self.text = date_ |
| 16 | + self._parse() |
| 17 | + |
| 18 | + def _parse(self): |
| 19 | + self.value: dt.date = dt.datetime.strptime(self.text, "%Y%m%d").date() |
| 20 | + |
| 21 | + |
| 22 | +class DateTime: |
| 23 | + def __init__(self, date_time_: str, tzinfo: dt.tzinfo = None, strict: bool = False): |
| 24 | + if not strict: |
| 25 | + date_time_ = date_time_.strip() |
| 26 | + self.text = date_time_ |
| 27 | + self.tzinfo = tzinfo |
| 28 | + self._parse() |
| 29 | + |
| 30 | + def _parse(self): |
| 31 | + try: |
| 32 | + _datetime = dt.datetime.strptime(self.text[:15], "%Y%m%dT%H%M%S") |
| 33 | + except ValueError as e: |
| 34 | + raise ParseError(f"'{self.text}' is not a valid DATE-TIME") from e |
| 35 | + |
| 36 | + if len(self.text) > 15 and self.text[15] == "Z": |
| 37 | + self.tzinfo = TzidRegistry.get("UTC") |
| 38 | + self.value = _datetime.replace(tzinfo=self.tzinfo) |
| 39 | + |
| 40 | + |
| 41 | +class Duration: |
| 42 | + def __init__(self, duration: str): |
| 43 | + self.text = duration.strip() |
| 44 | + self.value: dt.timedelta = dt.timedelta() |
| 45 | + self._parse() |
| 46 | + |
| 47 | + def _parse(self): |
| 48 | + if "," in self.text: |
| 49 | + raise ParseError("DURATION must have a single value.") |
| 50 | + |
| 51 | + interval_map = {"W": "weeks", "D": "days", "H": "hours", "M": "minutes", "S": "seconds"} |
| 52 | + |
| 53 | + _sign = -1 if self.text[0] == "-" else 1 |
| 54 | + params = {} |
| 55 | + for part in re.findall(r"\d{0,2}[PTWDHMS]{0,2}", self.text): |
| 56 | + if part and part[-1] in interval_map: |
| 57 | + params[interval_map[part[-1]]] = int(part[:-1]) |
| 58 | + if not params: |
| 59 | + raise ParseError(f"Invalid duration string : {self.text}") |
| 60 | + self.value = _sign * dt.timedelta(**params) |
| 61 | + |
| 62 | + |
| 63 | +class Period: |
| 64 | + def __init__(self, period: str, tzinfo: dt.tzinfo = None): |
| 65 | + self.text = period |
| 66 | + self.tzinfo = tzinfo |
| 67 | + |
| 68 | + self.is_explicit = False |
| 69 | + self.start_dt = None |
| 70 | + self.end_dt = None |
| 71 | + self.delta = None |
| 72 | + self._parse() |
| 73 | + |
| 74 | + def _parse(self): |
| 75 | + start_dt, end_dt = self.text.split("/") |
| 76 | + self.start_dt = DateTime(start_dt, self.tzinfo).value |
| 77 | + if _is_duration(end_dt): |
| 78 | + # period-start = date-time "/" dur-value |
| 79 | + self.is_explicit = False |
| 80 | + self.delta = Duration(end_dt).value |
| 81 | + else: |
| 82 | + # period-explicit = date-time "/" date-time |
| 83 | + self.is_explicit = True |
| 84 | + self.end_dt = DateTime(end_dt, self.tzinfo).value |
| 85 | + |
| 86 | + @property |
| 87 | + def value(self) -> tuple[dt.datetime, dt.datetime | dt.timedelta]: |
| 88 | + return self.start_dt, self.delta or self.end_dt |
| 89 | + |
| 90 | + |
| 91 | +class Time: |
| 92 | + def __init__(self, time: str, tzinfo: dt.tzinfo = None): |
| 93 | + self.text = time |
| 94 | + self.tzinfo = tzinfo |
| 95 | + self._parse() |
| 96 | + |
| 97 | + def _parse(self): |
| 98 | + try: |
| 99 | + _time = dt.datetime.strptime(self.text[:6], "%H%M%S").time() |
| 100 | + except ValueError as e: |
| 101 | + raise ParseError(f"'{self.text}' is not a valid TIME") from e |
| 102 | + |
| 103 | + if len(self.text) > 6 and self.text[6] == "Z": |
| 104 | + self.tzinfo = TzidRegistry.get("UTC") |
| 105 | + self.value = _time.replace(tzinfo=self.tzinfo) |
0 commit comments