| 1 |
"""Adapter for VEVENT.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import datetime |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
from recurring_ical_events.adapters.component import ComponentAdapter |
| 9 |
from recurring_ical_events.util import ( |
| 10 |
convert_to_datetime, |
| 11 |
is_date, |
| 12 |
normalize_pytz, |
| 13 |
) |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from recurring_ical_events.types import Time |
| 17 |
|
| 18 |
|
| 19 |
class EventAdapter(ComponentAdapter): |
| 20 |
"""An icalendar event adapter.""" |
| 21 |
|
| 22 |
@staticmethod |
| 23 |
def component_name() -> str: |
| 24 |
"""The icalendar component name.""" |
| 25 |
return "VEVENT" |
| 26 |
|
| 27 |
@property |
| 28 |
def end_property(self) -> str: |
| 29 |
"""DTEND""" |
| 30 |
return "DTEND" |
| 31 |
|
| 32 |
@property |
| 33 |
def raw_start(self) -> Time: |
| 34 |
"""Return DTSTART""" |
| 35 |
|
| 36 |
|
| 37 |
return self._component["DTSTART"].dt |
| 38 |
|
| 39 |
@property |
| 40 |
def raw_end(self) -> Time: |
| 41 |
"""Yield DTEND or calculate the end of the event based on |
| 42 |
DTSTART and DURATION. |
| 43 |
""" |
| 44 |
|
| 45 |
end = self._component.get("DTEND") |
| 46 |
if end is not None: |
| 47 |
return end.dt |
| 48 |
duration = self._component.get("DURATION") |
| 49 |
if duration is not None: |
| 50 |
start = self._component["DTSTART"].dt |
| 51 |
if duration.dt.seconds != 0 and is_date(start): |
| 52 |
start = convert_to_datetime(start, None) |
| 53 |
return normalize_pytz(start + duration.dt) |
| 54 |
start = self._component["DTSTART"].dt |
| 55 |
if is_date(start): |
| 56 |
return start + datetime.timedelta(days=1) |
| 57 |
return start |
| 58 |
|
| 59 |
|
| 60 |
__all__ = ["EventAdapter"] |