File size: 7,617 Bytes
550665c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
.. _recurrence:
Recurrence
==========
With ``gcsa`` you can create recurrent events. Use :py:mod:`~gcsa.recurrence` module.
There are 8 methods that you can use to define recurrence rules:
* :py:meth:`~gcsa.recurrence.Recurrence.rule` - rule that defines recurrence
* :py:meth:`~gcsa.recurrence.Recurrence.exclude_rule` - rule that defines excluded dates/datetimes
* :py:meth:`~gcsa.recurrence.Recurrence.dates` - date or list of dates to include
* :py:meth:`~gcsa.recurrence.Recurrence.exclude_dates` - date or list of dates to exclude
* :py:meth:`~gcsa.recurrence.Recurrence.times` - datetime or list of datetimes to include
* :py:meth:`~gcsa.recurrence.Recurrence.exclude_times` - datetime or list of datetimes to exclude
* :py:meth:`~gcsa.recurrence.Recurrence.periods` - period or list of periods to include
* :py:meth:`~gcsa.recurrence.Recurrence.exclude_periods` - period or list of periods to exclude
.. note:: Methods ``{method}`` have the same format and parameters as their ``exclude_{method}``
counterparts. So all examples for ``{method}`` also apply to ``exclude_{method}``.
These methods return strings in ``RRULE`` format that you can pass as a ``recurrence`` parameter
to the :py:class:`~gcsa.event.Event` objects. You can pass one string or list of strings.
For example:
.. code-block:: python
Event('Breakfast',
(1/Jan/2020)[9:00],
(1/Jan/2020)[10:00],
recurrence=Recurrence.rule(freq=DAILY))
or
.. code-block:: python
Event('Breakfast',
(1/Jan/2019)[9:00],
(1/Jan/2020)[9:00],
recurrence=[
Recurrence.rule(freq=DAILY),
Recurrence.exclude_rule(by_week_day=[SU, SA])
])
Examples
--------
You will need to import :py:class:`~gcsa.recurrence.Recurrence` class and optionally other
auxiliary classes and objects:
.. code-block:: python
from gcsa.recurrence import Recurrence
# days of the week
from gcsa.recurrence import SU, MO, TU, WE, TH, FR, SA
# possible repetition frequencies
from gcsa.recurrence import SECONDLY, MINUTELY, HOURLY, \
DAILY, WEEKLY, MONTHLY, YEARLY
`Earth Hour`_, which occurs on the last Saturday of March every year:
.. code-block:: python
from datetime import datetime
r = Recurrence.rule(freq=MONTHLY, interval=12, by_week_day=SA(-1))
start = datetime(year=2024, month=3, day=23, hour=20, minute=30)
event = Event("Earth hour", start=start, recurrence=r)
event = gc.add_event(event)
Following examples were taken from the `Internet Calendaring and Scheduling Core Object Specification (iCalendar)`_
and adapted to ``gcsa``.
`Daily for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=DAILY, count=10)
or as ``DAILY`` is a default frequency:
.. code-block:: python
Recurrence.rule(count=10)
`Every other day`:
.. code-block:: python
Recurrence.rule(freq=DAILY, interval=2)
`Every 10 days, 5 occurrences`:
.. code-block:: python
Recurrence.rule(count=5, interval=10)
`Every day in January`:
.. code-block:: python
Recurrence.rule(freq=YEARLY,
by_month=1,
by_week_day=[SU,MO,TU,WE,TH,FR,SA])
or
.. code-block:: python
Recurrence.rule(freq=DAILY, by_month=1)
`Weekly for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=WEEKLY, count=10)
`Weekly on Tuesday and Thursday`:
.. code-block:: python
Recurrence.rule(freq=WEEKLY,
by_week_day=[TU, TH])
`Every other week on Monday, Wednesday, and Friday`:
.. code-block:: python
Recurrence.rule(freq=WEEKLY,
interval=2,
by_week_day=[MO, WE, FR])
`Every other week on Tuesday and Thursday, for 8 occurrences`:
.. code-block:: python
Recurrence.rule(freq=WEEKLY,
interval=2,
count=8,
by_week_day=[TU, TH])
`Monthly on the first Friday for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
count=10,
by_week_day=FR(1))
`Every other month on the first and last Sunday of the month for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
interval=2,
count=10,
by_week_day=[SU(1), SU(-1)])
`Monthly on the second-to-last Monday of the month for 6 months`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
count=6,
by_week_day=MO(-2))
`Monthly on the third-to-the-last day of the month`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
by_month_day=-3)
`Monthly on the 2nd and 15th of the month for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
count=10,
by_month_day=[2, 15])
`Monthly on the first and last day of the month for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
count=10,
by_month_day=[1, -1])
`Every 18 months on the 10th thru 15th of the month for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
interval=18,
count=10,
by_month_day=list(range(10, 16)))
`Every Tuesday, every other month`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
interval=2,
by_week_day=TU)
`Yearly in June and July for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=YEARLY,
count=10,
by_month=[6, 7])
`Every third year on the 1st, 100th, and 200th day for 10 occurrences`:
.. code-block:: python
Recurrence.rule(freq=YEARLY,
interval=3,
count=10,
by_year_day=[1, 100, 200])
`Every 20th Monday of the year`:
.. code-block:: python
Recurrence.rule(freq=YEARLY,
by_week_day=MO(20))
`Monday of week number 20 (where the default start of the week is Monday)`:
.. code-block:: python
Recurrence.rule(freq=YEARLY,
by_week=20,
week_start=MO)
`Every Thursday in March`:
.. code-block:: python
Recurrence.rule(freq=YEARLY,
by_month=3,
by_week_day=TH)
`The third instance into the month of one of Tuesday, Wednesday, or
Thursday, for the next 3 months`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
count=3,
by_week_day=[TU, WE, TH],
by_set_pos=3)
`The second-to-last weekday of the month`:
.. code-block:: python
Recurrence.rule(freq=MONTHLY,
by_week_day=[MO, TU, WE, TH, FR],
by_set_pos=-2)
`Every 20 minutes from 9:00 AM to 4:40 PM every day`:
.. code-block:: python
Recurrence.rule(freq=DAILY,
by_hour=list(range(9, 17)),
by_minute=[0, 20, 40])
.. _`Internet Calendaring and Scheduling Core Object Specification (iCalendar)`: https://tools.ietf.org/html/rfc5545#section-3.8.5
.. _`Earth Hour`: https://www.earthhour.org/
|