diff --git a/news/876.feature.2 b/news/876.feature.2 new file mode 100644 index 000000000..49df8a9c3 --- /dev/null +++ b/news/876.feature.2 @@ -0,0 +1 @@ +Created the :attr:`vDuration.ical_value ` property. I used AI to help with this change. @arrudagba diff --git a/src/icalendar/prop/dt/duration.py b/src/icalendar/prop/dt/duration.py index b4f212e87..35d03c533 100644 --- a/src/icalendar/prop/dt/duration.py +++ b/src/icalendar/prop/dt/duration.py @@ -177,6 +177,11 @@ def dt(self) -> timedelta: """The time delta for compatibility.""" return self.td + @property + def ical_value(self) -> timedelta: + """Duration as a :class:`~datetime.timedelta` according to :rfc:`5545#section-3.3.6`.""" + return self.td + @classmethod def examples(cls) -> list[Self]: """Examples of vDuration.""" diff --git a/src/icalendar/tests/prop/test_vDuration.py b/src/icalendar/tests/prop/test_vDuration.py new file mode 100644 index 000000000..60823bad3 --- /dev/null +++ b/src/icalendar/tests/prop/test_vDuration.py @@ -0,0 +1,35 @@ +"""Test the ical_value property of vDuration.""" + +from datetime import timedelta + +import pytest + +from icalendar import Event +from icalendar.prop import vDuration + + +@pytest.mark.parametrize( + "td", + [ + timedelta(days=15, seconds=18020), + timedelta(weeks=7), + timedelta(hours=-5), + timedelta(days=-1, hours=-5), + timedelta(0), + ], +) +def test_ical_value(td): + """ical_value returns the timedelta of the duration.""" + assert vDuration(td).ical_value == td + + +def test_ical_value_from_string(): + """ical_value also works when the duration is created from a string.""" + assert vDuration("-P14D").ical_value == timedelta(days=-14) + + +def test_decoded_uses_ical_value(): + """Component.decoded() returns the ical_value of a DURATION.""" + event = Event() + event.add("DURATION", timedelta(hours=2)) + assert event.decoded("DURATION") == timedelta(hours=2)