Skip to content

Commit

Permalink
Merge pull request #750 from david-venhoff/main
Browse files Browse the repository at this point in the history
  • Loading branch information
niccokunzmann authored Nov 27, 2024
2 parents c1cfc62 + 18fbc30 commit a8b1ca9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Changelog

Minor changes:

- ...
- Add a ``weekday`` attribute to ``vWeekday`` components. See `Issue 749 <https://github.com/collective/icalendar/issues/749>`_.

Breaking changes:

- ...
- The ``relative`` attribute of ``vWeekday`` components has the correct sign now. See `Issue 749 <https://github.com/collective/icalendar/issues/749>`_.

New features:

Expand Down
1 change: 1 addition & 0 deletions docs/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ icalendar contributors
- Jeffrey Whewhetu <jeffwhewhetu@gmail.com>
- `Soham Dutta <https://github.com/NP-compete>`_
- `Serif OZ <https://github.com/SerifOZ>`_
- David Venhoff <https://github.com/david-venhoff>

Find out who contributed::

Expand Down
30 changes: 29 additions & 1 deletion src/icalendar/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,32 @@ def dt(self):


class vWeekday(str):
"""This returns an unquoted weekday abbrevation.
"""Either a ``weekday`` or a ``weekdaynum``
.. code-block:: pycon
>>> from icalendar import vWeekday
>>> vWeekday("MO") # Simple weekday
'MO'
>>> vWeekday("2FR").relative # Second friday
2
>>> vWeekday("2FR").weekday
'FR'
>>> vWeekday("-1SU").relative # Last Sunday
-1
Definition from RFC:
.. code-block:: text
weekdaynum = [[plus / minus] ordwk] weekday
plus = "+"
minus = "-"
ordwk = 1*2DIGIT ;1 to 53
weekday = "SU" / "MO" / "TU" / "WE" / "TH" / "FR" / "SA"
;Corresponding to SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
;FRIDAY, and SATURDAY days of the week.
"""
week_days = CaselessDict({
"SU": 0, "MO": 1, "TU": 2, "WE": 3, "TH": 4, "FR": 5, "SA": 6,
Expand All @@ -931,7 +956,10 @@ def __new__(cls, value, encoding=DEFAULT_ENCODING):
relative = match['relative']
if weekday not in vWeekday.week_days or sign not in '+-':
raise ValueError(f'Expected weekday abbrevation, got: {self}')
self.weekday = weekday or None
self.relative = relative and int(relative) or None
if sign == '-' and self.relative:
self.relative *= -1
self.params = Parameters()
return self

Expand Down
27 changes: 27 additions & 0 deletions src/icalendar/tests/prop/test_vWeekday.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from icalendar.prop import vWeekday


def test_simple():
weekday = vWeekday("SU")
assert weekday.to_ical() == b"SU"
assert weekday.weekday == "SU"
assert weekday.relative is None


def test_relative():
weekday = vWeekday("-1MO")
assert weekday.to_ical() == b"-1MO"
assert weekday.weekday == "MO"
assert weekday.relative == -1


def test_roundtrip():
assert vWeekday.from_ical(vWeekday("+2TH").to_ical()) == "+2TH"


def test_error():
"""Error: Expected weekday abbrevation, got: \"-100MO\" """
with pytest.raises(ValueError):
vWeekday.from_ical("-100MO")

0 comments on commit a8b1ca9

Please sign in to comment.