Debian Codegraph @

lib/Date/Manip/Recur.pod

raw
1 # Copyright (c) 1998-2026 Sullivan Beck. All rights reserved.
2 # This program is free software; you can redistribute it and/or modify it
3 # under the same terms as Perl itself.
4
5 =pod
6
7 =head1 NAME
8
9 Date::Manip::Recur - methods for working with recurring events
10
11 =head1 SYNOPSIS
12
13 use Date::Manip::Recur;
14 $date = new Date::Manip::Recur;
15
16 =head1 DESCRIPTION
17
18 This module contains functions useful in parsing and manipulating
19 recurrences. A recurrence is a notation for specifying when a recurring
20 event occurs. For example, if an event occurs every other Friday or
21 every 4 hours, this can be defined as a recurrence. A fully specified
22 recurrence consists of the following pieces of information:
23
24 =over 4
25
26 =item B<Frequency>
27
28 The most basic piece of information is the frequency. For relatively
29 simple recurring events, the frequency defines when those events
30 occur. For more complicated recurring events, the frequency tells
31 approximately when the events occur (but to get the actual
32 events, certain modifiers must be applied as described below).
33
34 Examples of recurring events include:
35
36 the first of every month
37 every other day
38 the 4th Thursday of each month at 2:00 PM
39 every 2 hours and 30 minutes
40
41 All of these can be expressed as a frequency.
42
43 NOTE: unlike date parsing, support for frequencies written out in
44 English (or whatever language you are working in) is extremely
45 limited. For example, the string "the first of every month" will NOT
46 be parsed as a valid frequency. A limited number of frequencies can be
47 expressed in a written out form (see the <L/"OTHER FREQUENCY FORMATS"
48 section below), but most must be expressed in the format described
49 below in <L/"FREQUENCY NOTATION">. In this document however, the written out
50 form will often be used for the sake of clarity.
51
52 Since a frequency typically refers to events that could happen an
53 infinite number of times, you usually have to specify a date range to
54 get the actual dates. Some frequencies also require a base date (i.e.
55 information about when one such even actually occurred) since the
56 frequency is otherwise ambiguous. For example, the frequency 'every
57 other day' does not include enough information to specify the dates
58 that the event happened on, so you have to explicitly define one of
59 them. Then all others can be derived.
60
61 =item B<Modifier>
62
63 Complex recurring events may require the use of modifiers in order to
64 get them correct.
65
66 For example, in America, many places treat both Thanksgiving and the
67 day after as holidays. Thanksgiving is easy to define since it is
68 defined as:
69
70 4th Thursday of every November
71
72 In the frequency notation (described below), this would be written as:
73
74 1*11:4:5:0:0:0
75
76 The day after Thanksgiving is NOT possible to define in the same way.
77 Depending on the year, the day after the 4th Thursday may be the 4th
78 or 5th Friday.
79
80 The only way to accurately define the day after Thanksgiving is to
81 specify a frequency and a modifier:
82
83 4th Thursday of every November
84 +1 day
85
86 In frequency notation, this can be expressed as:
87
88 1*11:4:5:0:0:0*FD1
89
90 The syntax for the various modifiers is described below in the
91 L</"MODIFIERS"> section.
92
93 =item B<Base date>
94
95 Many recurrences have a base date which is a date on which a recurring
96 event is based.
97
98 The base date is not necessarily a date where the recurring event
99 occurs. Instead, it may be modified (with modifiers, or with values
100 specified in the recurrence) to actually produce a recurring event.
101
102 For example, if the frequency is
103
104 every other Friday at noon
105
106 the base date will be a Friday and the recurring event will happen on
107 that Friday, Friday two weeks later, Friday four weeks later, etc.
108 In all cases, the dates will be modified to be at noon.
109
110 If the frequency has a modifier, such as:
111
112 every other Friday
113 + 1 day
114
115 (and yes, this trivial example could be expressed as the frequency
116 'every other Saturday' with no modifiers), then the base date is still
117 on a Friday, but the actual recurring event is determined by applying
118 modifiers and occurs on Saturday.
119
120 Recurring events are assigned a number with the event that is referred
121 to by the base date being the 0th occurrence, the first one after that
122 as the 1st occurrence, etc. Recurring events can also occur before
123 the base date with the last time the recurring event occurred before
124 the base date is the -1th occurrence.
125
126 So, if the frequency is
127
128 the first of every month
129
130 and the base date is 'Mar 1, 2000', then the 5 recurring events around
131 it are:
132
133 N Date
134
135 -2 Jan 1 2000
136 -1 Feb 1 2000
137 0 Mar 1 2000
138 +1 Apr 1 2000
139 +2 May 1 2000
140
141 In some cases, the Nth date may not be defined. For example, if the
142 frequency is:
143
144 the 31st of every month
145
146 and the base date is Mar 31, 2000, the 5 recurring events around it
147 are:
148
149 N Date
150
151 -2 Jan 31 2000
152 -1 undefined
153 0 Mar 31 2000
154 1 undefined
155 2 May 31 2000
156
157 As mentioned above, the base date is used to determine one of the
158 occurrences of the recurring event... but it may not actually be on
159 of those events.
160
161 As an example, for the recurring event:
162
163 every other Friday
164
165 a base date could be on a Friday, but it would also be possible to
166 have a base date on some other day of the week, and it could
167 unambiguously refer simply to a week, and the recurring event would
168 occur on Friday of that week.
169
170 In most cases, it won't be necessary to treat base dates with that
171 level of complexity, but with complicated recurring events, it may
172 be necessary. More information on how Date::Manip determines a
173 recurring event from a base date is given below in the section L</"BASE DATES">.
174
175 =item B<Range>
176
177 A date range is simply a starting and an ending date. When a range is
178 used (primarily in the dates method as described below), only
179 recurring events (with all modifiers applied) which happened on or
180 after the start date and on or before the end date are used.
181
182 For example, if the frequency was
183
184 the first of every month
185
186 and the start/end dates were Jan 1 2000 and May 31 2000, the list of
187 dates referred to would be:
188
189 Jan 1 2000
190 Feb 1 2000
191 Mar 1 2000
192 Apr 1 2000
193 May 1 2000
194
195 If no base date is specified, but a date range is specified, the start
196 date is used as the specified base date.
197
198 It should be noted that if both the range and base date are specified,
199 the range is not used to determine a base date. Also, the first time
200 the recurring event occurs in this range may NOT be the 0th occurrence
201 with respect to the base date, and that is allowed.
202
203 NOTE: both dates in the range and the base date must all be in the
204 same time zone, and use the same L<Date::Manip::Base> object.
205
206 An alternate definition of the range may also be used to specify that
207 the recurring events based only on the interval and BEFORE any
208 modifiers are applied fall in the range.
209
210 This definition is described in more detail below.
211
212 =back
213
214 =head1 FREQUENCY NOTATION
215
216 The syntax for specifying a frequency requires some explanation. It is
217 very concise, but contains the flexibility to express every single
218 type of recurring event I could think of.
219
220 The syntax of the frequency description is a colon separated list of
221 the format Y:M:W:D:H:MN:S (which stand for year, month, week, etc.).
222 One (and only one) of the colons may optionally be replaced by an
223 asterisk, or an asterisk may be prepended to the string. For example,
224 the following are all valid frequency descriptions:
225
226 1:2:3:4:5:6:7
227 1:2*3:4:5:6:7
228 *1:2:3:4:5:6:7
229
230 But the following are NOT valid because they contain more than one
231 asterisk:
232
233 1:2*3:4:5*6:7
234 *1:2:3:4:5:6*7
235
236 When an asterisk is included, the portion to the left of it is called
237 the interval, and refers to an approximate time interval between
238 recurring events. For example, if the interval of the frequency is:
239
240 1:2*
241
242 it means that the recurring event occurs approximately every 1 year
243 and 2 months. The interval is approximate because elements to the right of
244 the asterisk, as well as any modifiers included in the recurrence, will
245 affect when the events actually occur.
246
247 If no asterisks are included, then the entire recurrence is an interval.
248 For example,
249
250 0:0:0:1:12:0:0
251
252 refers to an event that occurs every 1 day, 12 hours.
253
254 The format of the interval is very simple. It is colon separated digits
255 only. No other characters are allowed.
256
257 The portion of the frequency that occur after an asterisk is called
258 the recurrence time (or rtime), and refers to a specific value (or
259 values) for that type of time element (i.e. exactly as it would appear
260 on a calendar or a clock). For example, if the frequency ends with
261 the rtime:
262
263 *12:0:0
264
265 then the recurring event occurs at 12:00:00 (noon).
266
267 For example:
268
269 0:0:0:2*12:30:0 every 2 days at 12:30 (each day)
270
271 Elements in the rtime can be listed as single values, ranges (2
272 numbers separated by a dash "-"), or a comma separated list of values
273 or ranges. In some cases, negative values are appropriate for the
274 week or day values. -1 stands for the last possible value, -2 for the
275 second to the last, etc.
276
277 If multiple values are included in more than one field in the rtime,
278 every possible combination will be used. For example, if the frequency
279 ends with the rtime:
280
281 *12-13:0,30:0
282
283 the event will occur at 12:00, 12:30, 13:00, and 13:30.
284
285 Some examples are:
286
287 0:0:0:1*2,4,6:0:0 every day at at 02:00, 04:00, and 06:00
288 0:0:0:2*12-13:0,30:0 every other day at 12:00, 12:30, 13:00,
289 and 13:30
290 0:1:0*-1:0:0:0 the last day of every month
291 *1990-1995:12:0:1:0:0:0
292 Dec 1 in 1990 through 1995
293
294 There is no way to express the following with a single recurrence:
295
296 every day at 12:30 and 1:00
297
298 You have to use two recurrences to do this.
299
300 You can include negative numbers in ranges. For example, including the
301 range -2--1 means to go from the 2nd to the last to the last
302 occurrence. Negative values are only supported in the week and
303 day fields, and only in some cases.
304
305 You can even use a range like 2--2 (which means to go from the 2nd to
306 the 2nd to the last occurrence). However, this is STRONGLY discouraged
307 since this leads to a date which produces a variable number of events.
308 As a result, the only way to determine the Nth date is to calculate
309 every date starting at the base date. If you know that every date
310 produces exactly 4 recurring events, you can calculate the Nth date
311 without needing to determine every intermediate date.
312
313 When specifying a range, the first value must be less than the second
314 or else nothing will be returned.
315
316 When both the week and day elements are non-zero and the day is right
317 of the asterisk, the day refers to the day of week. The following
318 examples illustrate these type of frequencies:
319
320 0:1*4:2:0:0:0 4th Tuesday (day 2) of every month
321 0:1*-1:2:0:0:0 last Tuesday of every month
322 0:0:3*2:0:0:0 every 3rd Tuesday (every 3 weeks
323 on 2nd day of week)
324 1:0*12:2:0:0:0 the 12th Tuesday of each year
325
326 NOTE: The day of week refers to the numeric value of each day as
327 specified by ISO 8601. In other words, day 1 is ALWAYS Monday, day 7 is
328 ALWAYS Sunday, etc., regardless of what day of the week the week is
329 defined to begin on (using the FirstDay config variable). So when the
330 day field refers to the day of week, it's value (or values if a range
331 or comma separated list are used) must be 1-7.
332
333 When the week element is zero and the month element is non-zero and
334 the day element is right of the asterisk, the day value is the day of
335 the month (it can be from 1 to 31 or -1 to -31 counting from the end
336 of the month).
337
338 3*1:0:2:12:0:0 every 3 years on Jan 2 at noon
339 0:1*0:2:12,14:0:0 2nd of every month at 12:00 and 14:00
340 0:1:0*-2:0:0:0 2nd to last day of every month
341
342 NOTE: If the day given refers to the 29th, 30th, or 31st, in a month
343 that does not have that number of days, it is ignored. For example, if
344 you ask for the 31st of every month, it will return dates in Jan, Mar,
345 May, Jul, etc. Months with fewer than 31 days will be ignored.
346
347 If both the month and week elements are zero, and the year element is
348 non-zero, the day value is the day of the year (1 to 365 or 366 -- or
349 the negative numbers to count backwards from the end of the year).
350
351 1:0:0*45:0:0:0 45th day of every year
352
353 Specifying a day that doesn't occur in that year silently ignores that
354 year. The only result of this is that specifying +366 or -366 will ignore
355 all years except leap years.
356
357 If the week element is non-zero and to the right of the asterisk, and
358 the day element is zero, the frequency refers to the first day of the
359 given week of the month or week of the year:
360
361 0:1*2:0:0:0:0 the first day of the 2nd week of
362 every month
363 1:0*2:0:0:0:0 the first day of the 2nd week of
364 every year
365
366 Although the meaning of almost every recurrence can be deduced by the
367 above rules, a set of tables describing every possible combination of Y/M/W/D
368 meanings, and giving an example of each is included below in the
369 section L</"LIST OF Y/M/W/D FREQUENCY DEFINITIONS">. It also explains a small
370 number of special cases.
371
372 NOTE: If all fields left of the asterisk are zero, the last one is
373 implied to be 1. In other words, the following are equivalent:
374
375 0:0:0*x:x:x:x
376 0:0:1*x:x:x:x
377
378 and can be thought of as every possible occurrence of the rtime.
379
380 NOTE: When applying a frequency to get a list of dates on which a
381 recurring event occurs, a delta is created from the frequency which is
382 applied to get dates referred to by the interval. These are then
383 operated on by the rtime and by modifiers to actually get the
384 recurring events. The deltas will always be exact or approximate.
385 There is no support for business mode recurrences. However, with the
386 careful use of modifiers (discussed below), most recurring business
387 events can be determined too.
388
389 =head1 BASE DATES
390
391 A recurrence of the form *Y:M:W:D:H:MN:S (which is technically speaking
392 not a recurring event... it is just a date or dates specified using the
393 frequency syntax) uses the first date which matches the frequency as the
394 base date. Any base date specified will be completely ignored. A date range
395 may be specified to work with a subset of the dates.
396
397 All other recurrences use a specified base date in order to determine
398 when the 0th occurrence of a recurring event happens. As mentioned
399 above, the specified base date may be determined from the start date,
400 or specified explicitly.
401
402 The specified base date is used to provide the bare minimum
403 information. For example, the recurrence:
404
405 0:0:3*4:0:0:0 every 3 weeks on Thursday
406
407 requires a base date to determine the week, but nothing else. Using the
408 standard definition (Monday-Sunday) for a week, and given that one week in
409 August 2009 is Aug 10 to Aug 16, any date in the range Aug 10 to Aug 16 will
410 give the same results. The definition of the week defaults to Monday-Sunday,
411 but may be modified using the FirstDay config variable.
412
413 Likewise, the recurrence:
414
415 1:3*0:4:0:0:0 every 1 year, 3 months on the 4th
416 day of the month
417
418 would only use the year and month of the base date, so all dates in a given
419 month would give the same set of recurring dates.
420
421 It should also be noted that a date may actually produce multiple
422 recurring events. For example, the recurrence:
423
424 0:0:2*4:12,14:0:0 every 2 weeks on Thursday at 12:00
425 and 14:00
426
427 produces 2 events for every date. So in this case, the base date produces the
428 0th and 1st event, the base date + an offset produces the 2nd and 3rd events,
429 etc.
430
431 It must be noted that the base date refers ONLY to the interval part of the
432 recurrence. The rtime and modifiers are NOT used in determining the base
433 date.
434
435 =head1 INTERVAL
436
437 The interval of a frequency (everything left of the asterisk) will be used
438 to generate a list of dates (called interval dates). When rtime values and
439 modifiers are applied to an interval date, it produces the actual recurring
440 events.
441
442 As already noted, if the rtime values include multiple values for any
443 field, more than one event are produced by a single interval date.
444
445 It is important to understand is how the interval dates are
446 calculated. The interval is trivially turned into a delta. For example,
447 with the frequency 0:0:2*4:12:0:0, the interval is 0:0:2 which
448 produces the delta 0:0:2:0:0:0:0.
449
450 In order to get the Nth interval date, the delta is multiplied by N and
451 added to the base date. In other words:
452
453 D(0) = Jan 31
454 D(1) = Jan 31 + 1 month = Feb 28
455 D(2) = Jan 31 + 2 month = Mar 31
456
457 =head1 DATE RANGE
458
459 The start and end dates form the range in which recurring events can fall
460 into.
461
462 Every recurring date will fall in the limit:
463
464 start <= date <= end
465
466 When a recurrence is created, it may include a default range, and this is
467 handled by the RecurRange config variable.
468
469 By default, the date range applies to the final dates once all modifiers
470 have been applied.
471
472 This behavior can be changed by applying the range to the unmodified
473 dates.
474
475 An example of how this applies might be in defining New Year's Day
476 (observed). The most useful definition of this would be:
477
478 1*1:0:1:0:0:0*DWD
479
480 which means Jan 1 modified to the nearest working day.
481
482 But if you wanted to find New Year's for 2005 using this definition by passing
483 in a start date of 2005-01-01-00:00:00 and an end date of 2005-12-31-23:59:59,
484 you won't find anything because New Year's day will actually be observed on
485 2004-12-31 (since Jan 1 is a Saturday).
486
487 To get around this, you can pass in a non-zero parameter with the recurrence
488 which means that this range will be applied to the unmodified dates.
489
490 In effect, this discards the modifier (DWD), gets the dates that fall in
491 the range, and for all that fall in the range, the modifiers are applied.
492
493 So:
494
495 1*1:0:1:0:0:0*DWD**2005-01-01-00:00:00*2005-12-31-23:59:59
496
497 will return no dates, but:
498
499 1*1:0:1:0:0:0*DWD**2005-01-01-00:00:00*2005-12-31-23:59:59*1
500
501 will return:
502
503 2004-12-31-00:00:00
504
505 =head1 OTHER FREQUENCY FORMATS
506
507 There are a small handful of English strings (or the equivalent in
508 other languages) which can be parsed in place of a numerical
509 frequency. These include:
510
511 every Tuesday in June [1997]
512 2nd Tuesday in June [1997]
513 last Tuesday in June [1997]
514
515 every Tuesday of every month [in 1997]
516 2nd Tuesday of every month [in 1997]
517 last Tuesday of every month [in 1997]
518
519 every day of every month [in 1997]
520 2nd day of every month [in 1997]
521 last day of every month [in 1997]
522
523 every day [in 1997]
524 every 2nd day [in 1977]
525 every 2 days [in 1977]
526
527 Each of these set the frequency. If the year is include in the string,
528 it also sets the dates in the range to be the first and last day of
529 the year.
530
531 In each of these, the numerical part (i.e. 2nd in all of the examples above)
532 can be any number from 1 to 31. To make a frequency with a larger number than
533 that, you have to use the standard format discussed above.
534
535 Due to the complexity of writing out (and parsing) frequencies written out,
536 I do not intend to add additional frequency formats, and the use of these
537 is discouraged. The frequency format described above is preferred.
538
539 =head1 MODIFIERS
540
541 Any number of modifiers may be added to a frequency to get the actual
542 date of a recurring event. Modifiers are case sensitive.
543
544 =over 4
545
546 =item B<Modifiers to set the day-of-week>
547
548 The following modifiers can be used to adjust a date to a specific
549 day of the week.
550
551 PDn Means the previous day n not counting today
552 PTn Means the previous day n counting today
553 NDn Means the next day n not counting today
554 NTn Means the next day n counting today
555 WDn Day n (1-7) of the current week
556
557 In each of these, 'n' is 1-7 (1 being Sunday, 7 being Saturday).
558
559 For example, PD2/ND2 returns the previous/next Tuesday. If the
560 date that this is applied to is Tuesday, it modifies it to one
561 week in the past/future.
562
563 PT2/NT2 are similar, but will leave the date unmodified if
564 it is a Tuesday.
565
566 =item B<Modifiers to move forward/backward a number of days>
567
568 These modifiers can be used to add/subtract n days to a date.
569
570 FDn Means step forward n days.
571 BDn Means step backward n days.
572
573 =item B<Modifiers to force events to be on business days>
574
575 Modifiers can also be used to force recurring events to occur
576 on business days. These modifiers include:
577
578 FWn Means step forward n workdays.
579 BWn Means step backward n workdays.
580
581 CWD The closest work day (using the TomorrowFirst
582 config variable).
583 CWN The closest work day (looking forward first).
584 CWP The closest work day (looking backward first).
585
586 NWD The next work day counting today
587 PWD The previous work day counting today
588 DWD The closest work day (using the TomorrowFirst config
589 variable) counting today
590
591 IBD This discards the date if it is not a business day.
592 NBD This discards the date if it IS a business day.
593
594 IWn This discards the date if it is not the n'th day
595 of the week (n=1-7, 1 is Monday)
596 NWn This discards the date if it IS the n'th day of the week
597
598 The CWD, CWN, and CWP modifiers will always change the date to the
599 closest working day NOT counting the current date.
600
601 The NWD, PWD, and DWD modifiers always change the date to the closest
602 working day unless the current date is a work day. In that case,
603 it is left unmodified.
604
605 CWD, CWN, and CWP will usually return the same value, but if you are
606 starting at the middle day of a 3-day weekend (for example), it will
607 return either the first work day of the following week, or the last
608 work day of the previous week depending on whether it looks forward or
609 backward first.
610
611 All business day modifiers ignore the time, so if a date is initially
612 calculated at Saturday at noon, and the FW1 is applied, the date is
613 initially moved to the following Monday (assuming it is a work day)
614 and the FW1 moves it to Tuesday. The final result will be Tuesday at
615 noon.
616
617 The IBD, NBD, IWn, and NWn modifiers eliminate dates from the list immediately.
618 In other words, if a recurrence has three modifiers:
619
620 FD1,IBD,FD1
621
622 then as a date is being tested, first the FD1 modifier is applied.
623 Then, it is tested to see if it is a business day. If it is, the
624 second FD1 modifier will be applied. Otherwise, the date will not
625 be included in the list of recurring events.
626
627 =item B<Special modifiers>
628
629 The following modifiers do things that cannot be expressed using any other
630 combination of frequency and modifiers:
631
632 EASTER Set the date to Easter for this year.
633
634 =back
635
636 =head1 DETERMINING DATES
637
638 In order to get a list of dates referred to by the recurrence, the
639 following steps are taken.
640
641 =over 4
642
643 =item B<The recurrence is tested for errors>
644
645 The recurrence must be completely specified with a base date (either
646 supplied explicitly, or derived from a start date) and date range when
647 necessary. All dates must be valid.
648
649 =item B<The actual base date is determined>
650
651 Using information from the interval and the specified base date, the
652 actual base date is determined.
653
654 =item B<The Nth date is calculated>
655
656 By applying the delta that corresponds to the interval, and then
657 applying rtime and modifier information, the Nth date is determined.
658
659 This is repeated until all desired dates have been obtained.
660
661 The nth method described below has more details.
662
663 =item B<The range is tested>
664
665 Any date that fall outside the range is discarded.
666
667 NOTE: when the recurrence contains no interval, it is not necessary to
668 specify the range, and if it is not specified, all of the dates are
669 used. The range MAY be specified to return only a subset of the dates
670 if desired.
671
672 =back
673
674 =head1 LIST OF Y/M/W/D FREQUENCY DEFINITIONS
675
676 Because the week and day values may have multiple meanings depending
677 on where the asterisk is, and which of the fields have non-zero values,
678 a list of every possible combination is included here (though most can
679 be determined using the rules above).
680
681 When the asterisk occurs before the day element, and the day element
682 is non-zero, the day element can take on multiple meanings depending
683 on where the asterisk occurs, and which leading elements (year, month,
684 week) have non-zero values. It can refer to the day of the week, day
685 of the month, or day of the year.
686
687 When the asterisk occurs before the week element, the week element of
688 the frequency can also take on multiple meanings as well. When the month
689 field and day fields are zero, it refers to the week of the year. Since
690 the week of the year is well defined in the ISO 8601 spec, there is
691 no ambiguity.
692
693 When the month field is zero, but the day field is not, the week field
694 refers to the nth occurrence of the day of week referred to by the
695 day field in the year.
696
697 When the month field is non-zero, the week field refers to the nth
698 occurrence of the day of week in the month.
699
700 In the tables below only the first 4 elements of the frequency are
701 shown. The actual frequency will include the hour, minute, and second
702 elements in addition to the ones shown.
703
704 When all elements left of the asterisk are 0, the interval is such
705 that it occurs the maximum times possible (without changing the type
706 of elements to the right of the asterisk). Another way of looking at
707 it is that the last 0 element of the interval is changed to 1. So, the
708 interval:
709
710 0:0*3:0
711
712 is equivalent to
713
714 0:1*3:0
715
716 When the year field is zero, and is right of the asterisk, it
717 means the current year.
718
719 =over 4
720
721 =item B<All elements left of the asterisk>
722
723 When all of the month, week, and day elements are left of the
724 asterisk, the simple definitions of the frequency are used:
725
726 frequency meaning
727
728 1:2:3:4 every 1 year, 2 months, 3 weeks,
729 4 days
730
731 Any, or all of the fields can be zero.
732
733 =item B<Non-zero day, non-zero week>
734
735 When both the day and week elements are non-zero, the day element
736 always refers to the day of week. Values must be in the range (1 to 7)
737 and no negative values are allowed.
738
739 The following tables shows all possible variations of the frequency
740 where this can happen (where day 4 = Thursday).
741
742 When the week is left of the asterisk, the interval is used to get the
743 weeks on the calendar containing a recurring date, and the day is used
744 to set the day of the week. The following are possible:
745
746 frequency meaning
747
748 1:2:3*4 every 1 year, 2 months, 3 weeks
749 on Thur
750
751 1:0:3*4 every 1 year, 3 weeks on Thur
752
753 0:2:3*4 every 2 months, 3 weeks on Thur
754
755 0:0:3*4 every 3 weeks on Thur
756
757 When the week is right of the asterisk, and a non-zero month is left of the
758 asterisk, the recurrence refers to a specific occurrence of a day-of-week
759 during a month. The following are possible:
760
761 frequency meaning
762
763 1:2*3:4 every 1 year, 2 months on the
764 3rd Thursday of the month
765
766 0:2*3:4 every 2 months on the 3rd Thur
767 of the month
768
769 When the week and month are both non-zero and right of the asterisk, the
770 recurrence refers to an occurrence of day-of-week during the given month.
771 Possibilities are:
772
773 frequency meaning
774
775 1*2:3:4 every 1 year in February on
776 the 3rd Thur
777
778 0*2:3:4 same as 1*2:3:4
779
780 *1:2:3:4 in Feb 0001 on the 3rd Thur
781 of the month
782
783 *0:2:3:4 on the 3rd Thur of Feb in the
784 current year
785
786 When the week is right of the asterisk, and the month is zero, the
787 recurrence refers to an occurrence of the day-of-week during the
788 year. The following are possible:
789
790 frequency meaning
791
792 1:0*3:4 every 1 year on the 3rd Thursday
793 1*0:3:4 of the year
794
795 *1:0:3:4 in 0001 on the 3rd Thur of
796 the year
797
798 0*0:3:4 same as 1*0:3:4
799
800 *0:0:3:4 on the 3rd Thur of the current
801 year
802
803 There is one special case:
804
805 frequency meaning
806
807 0:0*3:4 same as 0:1*3:4 (every month on
808 the 3rd Thur of the month)
809
810 =item B<Non-zero day, non-zero month>
811
812 When a non-zero day element occurs to the right of the asterisk and
813 the week element is zero, but the month element is non-zero, the day
814 elements always refers to a the day of month in the range (1 to 31)
815 or (-1 to -31).
816
817 The following table shows all possible variations of the frequency
818 where this can happen:
819
820 frequency meaning
821
822 1:2:0*4 every 1 year, 2 months on the
823 1:2*0:4 4th day of the month
824
825 1*2:0:4 every year on Feb 4th
826
827 *1:2:0:4 Feb 4th, 0001
828
829 0:2:0*4 every 2 months on the 4th day
830 0:2*0:4 of the month
831
832 0*2:0:4 same as 1*2:0:4
833
834 *0:2:0:4 Feb 4th of the current year
835
836 =item B<Zero day, non-zero week>
837
838 When a day is zero, and the week is non-zero, the recurrence refers
839 to a specific occurrence of the first day of the week (as given by
840 the FirstDay variable).
841
842 The frequency can refer to an occurrence of FirstDay in a specific
843 week (if the week is left of the asterisk):
844
845 frequency meaning
846
847 1:2:3*0 every 1 year, 2 months, 3 weeks on
848 FirstDay
849
850 1:0:3*0 every 1 year, 3 weeks on FirstDay
851
852 0:2:3*0 every 2 months, 3 weeks on FirstDay
853
854 0:0:3*0 every 3 weeks on FirstDay
855
856 or to a week in the year (if the week is right of the asterisk, and the
857 month is zero):
858
859 frequency meaning
860
861 1:0*3:0 every 1 year on the first day of the
862 1*0:3:0 3rd week of the year
863
864 *1:0:3:0 the first day of the 3rd week of 0001
865
866 or to an occurrence of FirstDay in a month (if the week is right of the
867 asterisk and month is non-zero):
868
869 frequency meaning
870
871 1:2*3:0 every 1 year, 2 months on the 3rd
872 occurrence of FirstDay
873
874 0:2*3:0 every 2 months on the 3rd occurrence
875 of FirstDay
876
877 1*2:3:0 every year on the 3rd occurrence
878 of FirstDay in Feb
879
880 0*2:3:0 same as 1*2:3:0
881
882 *1:2:3:0 the 3rd occurrence of FirstDay
883 Feb 0001
884
885 *0:2:3:0 the 3rd occurrence of FirstDay
886 in Feb of the current year
887
888 NOTE: in the last group, a slightly more intuitive definition of these
889 would have been to say that the week field refers to the week of the
890 month, but given the ISO 8601 manner of defining when weeks start,
891 this definition would have virtually no practical application. So the
892 definition of the week field referring to the Nth occurrence of
893 FirstDay in a month was used instead.
894
895 There are a few special cases here:
896
897 frequency meaning
898
899 0:0*3:0 same as 0:1*3:0 (every month on the 3rd
900 occurrence of the first day of week)
901
902 0*0:3:0 same as 1*0:3:0
903
904 *0:0:3:0 the first day of the 3rd week of the
905 current year
906
907 =item B<Non-zero day>
908
909 When a non-zero day element occurs and both the month and week
910 elements are zero, the day elements always refers to a the day of year
911 (1 to 366 or -1 to -366 to count from the end).
912
913 The following table shows all possible variations of the frequency
914 where this can happen:
915
916 frequency meaning
917
918 1:0:0*4 every year on the 4th day of
919 1:0*0:4 the year
920 1*0:0:4
921
922 *1:0:0:4 the 4th day of 0001
923
924 Other non-zero day variations have multiple meanings for the day
925 element:
926
927 frequency meaning
928
929 0:0:0*4 same as 0:0:1*4 (every week on Thur)
930
931 0:0*0:4 same as 0:1*0:4 (every month on the 4th)
932
933 0*0:0:4 same as 1*0:0:4
934
935 *0:0:0:4 the 4th day of the current year
936
937 =item B<All other variations>
938
939 The remaining variations have zero values for both week and day.
940 They are:
941
942 frequency meaning
943
944 1:2:0*0 every 1 year, 2 months on the first
945 1:2*0:0 day of the month
946
947 1*2:0:0 every year on Feb 1
948
949 *1:2:0:0 Feb 1, 0001
950
951 1:0:0*0 every 1 year on Jan 1
952 1:0*0:0
953 1*0:0:0
954
955 *1:0:0:0 Jan 1, 0001
956
957 0:2:0*0 every 2 months on the first day of
958 0:2*0:0 the month
959
960 0*2:0:0 same as 1*2:0:0
961
962 *0:2:0:0 Feb 1 of the current year
963
964 0:0:0*0 same as 0:0:1*0 (every week on
965 the first day of the week)
966
967 0:0*0:0 same as 0:1*0:0 (every month
968 on the 1st)
969
970 0*0:0:0 same as 1*0:0:0
971
972 *0:0:0:0 Jan 1 of the current year
973
974 =back
975
976 =head1 METHODS
977
978 =over 4
979
980 =item B<new>
981
982 =item B<new_config>
983
984 =item B<new_date>
985
986 =item B<new_delta>
987
988 =item B<new_recur>
989
990 =item B<base>
991
992 =item B<tz>
993
994 =item B<is_date>
995
996 =item B<is_delta>
997
998 =item B<is_recur>
999
1000 =item B<config>
1001
1002 =item B<err>
1003
1004 Please refer to the L<Date::Manip::Obj> documentation for these methods.
1005
1006 =item B<parse>
1007
1008 $err = $recur->parse($string [,$modifiers] [,$base,$start,$end,$unmod]);
1009
1010 This creates a new recurrence. A string containing a valid frequency
1011 is required. In addition, C<$start>, C<$end>, and C<$base> dates can be passed
1012 in (either as L<Date::Manip::Date> objects, or as strings containing dates
1013 that can be parsed), and any number of the modifiers listed above.
1014
1015 If the C<$start> or C<$end> dates are not included, they may be supplied
1016 automatically, based on the value of the RecurRange variable. If any
1017 of the dates are passed in, they must be included in the order given
1018 (though it is safe to pass an empty string or undef in for any of them
1019 if you only want to set some, but not all of them). If C<$unmod> is true,
1020 the range will apply to unmodified dates rather than the modified dates.
1021
1022 The C<$modifiers> argument must contain valid modifiers, or be left out of
1023 the argument list entirely. You cannot pass an empty string or undef in for it.
1024
1025 $err = $recur->parse($string);
1026
1027 This creates a recurrence from a string which contains all of the
1028 necessary elements of the recurrence. The string is of the format:
1029
1030 FREQ*MODIFIERS*BASE*START*END*UNMOD
1031
1032 where FREQ is a string containing a frequency, MODIFIERS is a string
1033 containing a comma separated list of modifiers, BASE, START, and END
1034 are strings containing parseable dates.
1035
1036 All pieces are optional, but order must be maintained, so all of the
1037 following are valid:
1038
1039 FREQ*MODIFIERS
1040 FREQ**BASE
1041 FREQ**BASE*START*END
1042 FREQ***START*END*UNMOD
1043
1044 If a part of the recurrence is passed in both as part of C<$string> and
1045 as an argument, the argument overrides the string portion, with the
1046 possible exception of modifiers. The modifiers in the argument override
1047 the string version unless the first one is a '+' in which case they
1048 are appended. See the modifiers method below for more information.
1049
1050 =item B<frequency>
1051
1052 =item B<start>
1053
1054 =item B<end>
1055
1056 =item B<basedate>
1057
1058 =item B<modifiers>
1059
1060 You can also create a recurrency in steps (or replace parts of an existing
1061 recurrence) using the following:
1062
1063 $err = $recur->frequency($frequency);
1064
1065 $err = $recur->start($start);
1066 $err = $recur->start($start,$unmod);
1067 $err = $recur->end($end);
1068
1069 $err = $recur->basedate($base);
1070
1071 $err = $recur->modifiers($modifiers);
1072 $err = $recur->modifiers(@modifiers);
1073
1074 These set the appropriate part of the recurrence.
1075
1076 Calling the frequency method discards all information currently
1077 stored in the Recur object (including an existing start, end, and
1078 base date), so this method should be called first.
1079
1080 In the modifiers method, the modifiers can be passed in as a string
1081 containing a comma separated list of modifiers, or as a list of
1082 modifiers. The modifiers passed in override all previously set
1083 modifiers UNLESS the first one is the string "+", in which case the
1084 new modifiers are appended to the list.
1085
1086 In the start, end, and base methods, the date passed in can be a
1087 L<Date::Manip::Date> object, or a string that can be parsed to get a date.
1088 If C<$unmod> is true, it will mean that the range will apply to unmodified
1089 dates.
1090
1091 NOTE: the parse method will overwrite all parts of the recurrence,
1092 so it is not appropriate to do:
1093
1094 $recur->modifiers($modifiers);
1095 $recur->parse($string);
1096
1097 The modifiers passed in in the first call will be overwritten.
1098
1099 These functions can also be used to look up the values.
1100
1101 $freq = $recur->frequency();
1102 $start = $recur->start();
1103 $end = $recur->end();
1104 @mods = $recur->modifiers();
1105
1106 ($base,$actual) = $recur->basedate();
1107
1108 The basedate function will return both the specified base and the actual
1109 base dates.
1110
1111 If any of the values are not yet determined, nothing will be returned.
1112
1113 =item B<dates>
1114
1115 @dates = $recur->dates([$start,$end,$unmod]);
1116
1117 Returns the list of dates defined by the full recurrence. If there is
1118 an error, or if there are no dates, an empty list will be returned.
1119
1120 C<$start> and C<$end> are either C<undef,> or dates which can be used to limit
1121 the set of dates passed back (they can be L<Date::Manip::Date> objects
1122 or strings that can be parsed).
1123
1124 If the recurrence does not have a start and end date already, passing
1125 in C<$start> and C<$end> will set the range (but they will NOT be stored in
1126 the recurrence).
1127
1128 If the recurrence does have a start and end date stored in it, the
1129 C<$start> and C<$end> arguments can be used to temporarily override
1130 the limits. For example, if a recurrence has a start date of
1131 Jan 1, 2006 00:00:00 and and end date of Dec 31, 2006 23:59:59 stored
1132 in the recurrence, passing in $start of Jul 1, 2006 00:00:00 will
1133 limit the dates returned to the range of Jul 1 to Dec 31.
1134
1135 Passing in a start date of Jul 1, 2007 will mean that no dates are
1136 returned since the recurrence limits the date to be in 2006.
1137
1138 If one or both of C<$start> and C<$end> are C<undef>, then the stored values
1139 will be used.
1140
1141 =item B<nth>
1142
1143 ($date,$err) = $recur->nth($n);
1144
1145 This returns the C<$n>th recurring event (C<$n> may be any integer). If
1146 an error occurs, it is returned (but it is not set in C<$recur> since
1147 it may be properly, though perhaps incompletely, defined). The following
1148 errors may be returned:
1149
1150 Invalid recurrence
1151 The recurrence has an error flag set.
1152
1153 Incomplete recurrence
1154 The recurrence is incomplete. It needs either a
1155 base date or a date range.
1156
1157 Range invalid
1158 The recurrence has an invalid date range (i.e.
1159 the end date occurs before the start date).
1160
1161 Start invalid
1162 End invalid
1163 Base invalid
1164 An invalid date was entered for one of the dates.
1165
1166 Not found
1167 In some cases, a recurrence may appear valid, but
1168 does not refer to any actual occurrences. If no
1169 dates are found within a certain number of attempts
1170 (given by the MaxRecurAttempts config variable), this
1171 error is returned.
1172
1173 There are a few special circumstances to be aware of.
1174
1175 1) If the recurrence contains no interval (i.e. is of the form
1176 *Y:M:W:D:H:MN:S), the dates come directly from the rtime values.
1177 In this case, the 0th event is the first date in the list of
1178 dates specified by the rtime. As such, C<$n> must be a positive
1179 integer. If C<$n> is negative, or outside the range of dates
1180 specified, the returned date will be C<undef> (but this is not
1181 an error).
1182
1183 2) A very small number of recurrences have an unknown number of
1184 recurring events associated with each date. This only happens if one
1185 of the values in the rtime is specified as a range including both a
1186 positive and negative index. For example, if the day field in an
1187 rtime refers to the day of month, and is 15--15 (i.e. the 15th day to
1188 the 15th to the last day), this may include 3 events (on a month with
1189 31 days), 2 event (months with 30 days), 1 event (months with 29
1190 days), or 0 events (months with 28 days). As such, in order to
1191 calculate the Nth date, you have to start with the 0th (i.e. base)
1192 date and calculate every event until you get the Nth one. For this
1193 reason, it is highly recommended that this type of frequency be
1194 avoided as it will be quite slow.
1195
1196 3) Most recurrences have a known number of events (equal to the number
1197 of combinations of values in the rtime) for each date. For these,
1198 calculating the Nth date is much faster. However, in this case, some
1199 of them may refer to an invalid date. For example, if the frequency is
1200 'the 31st of every month' and the base (0th) date is Jan 31, the 1st
1201 event would refer to Feb 31. Since that isn't valid, C<undef> would be
1202 returned for C<$n=1.> Obviously, it would be possible to actually
1203 determine the Nth valid event by calculating all N-1 dates, but in the
1204 interest of performance, this is not done.
1205
1206 4) The way the Nth recurring event is calculated differs slightly for
1207 NE>0 and NE<lt>0 if the delta referred to by the frequency is
1208 approximate. To calculate the Nth recurring event (where N>0), you
1209 take the base date and add N*DELTA (where DELTA is the delta
1210 determined by the frequency). To get the Nth recurring event (where
1211 NE<lt>0), a date is determine which, if N*DELTA were added to it,
1212 would produce the base date. For more details, refer to the
1213 L<Date::Manip::Calc> document. In the
1214 L<Date::Manip::Calc/"SUBTRACTION"> section in the discussion of
1215 approximate date-delta calculations, calculations are done with
1216 $subtract = 2.
1217
1218 =item B<next>
1219
1220 =item B<prev>
1221
1222 ($date,$err) = $recur->next();
1223 ($date,$err) = $recur->prev();
1224
1225 These return the next/previous recurring event.
1226
1227 The first time next/prev is called, one of the recurring events
1228 will be selected and returned (using the rules discussed below).
1229 Subsequent calls to next/prev will return the next or previous
1230 event.
1231
1232 Unlike the B<nth> method which will return a specific event (or
1233 undef if the Nth even is not defined), the next and prev methods
1234 will only work with defined events.
1235
1236 So, for the recurrence:
1237
1238 the 31st of every month
1239
1240 next might return the following sequence of events:
1241
1242 Jan 31 2000
1243 Mar 31 2000
1244 May 31 2000
1245
1246 The rules for determining what event to return the first time one
1247 of these is called are as follows:
1248
1249 1) If there is a range, next will return the first event that occurs
1250 after the start of the range. prev will return the last event that
1251 occurs before the end of the range.
1252
1253 2) If there is no range, next will return the first event on or after
1254 the base date. prev will return the last event before the base date.
1255
1256 The error codes are the same as for the nth method.
1257
1258 =back
1259
1260 =head1 HISTORY OF THE FREQUENCY NOTATION
1261
1262 I realize that the frequency notation described above looks quite
1263 complicated at first glance, but it is (IMO) the best notation for
1264 expressing recurring events in existence. I actually consider it the
1265 single most important contribution to date/time handling in
1266 Date::Manip.
1267
1268 When I first decided to add recurring events to Date::Manip, I first
1269 came up with a list of common ways of specifying recurring events, and
1270 then went looking for a notation that could be used to define them. I
1271 was hoping for a notation that would be similar to cron notation, but
1272 more powerful.
1273
1274 After looking in several specifications (including ISO 8601) and after
1275 a discussion on a mailing list of calendar related topics, it appeared
1276 that there was no concise, flexible notation for handling recurring
1277 events that would handle all of the common forms I'd come up with.
1278
1279 So, as a matter of necessity, I set about inventing my own notation.
1280 As I was looking at my list, it struck me that all of the parts which
1281 specified a frequency were higher level (i.e. referred to a larger
1282 unit of time) than those parts which specified a specific value (what
1283 I've called the rtime). In other words, when the terms were laid out
1284 from year down to seconds, the frequency part was always left of
1285 specific values.
1286
1287 That led immediately to the notation described above, so I started analyzing
1288 it to figure out if it could express all of the recurring events I'd
1289 come up with. It succeeded on 100% of them. Not only that, but by playing
1290 with different values (especially different combinations of m/w/d values), I
1291 found that it would define recurring events that I hadn't even thought of,
1292 but which seemed perfectly reasonable in hindsight.
1293
1294 After a very short period, I realized just how powerful this notation was,
1295 and set about implementing it, and as I said above, of all the contributions
1296 that Date::Manip has made, I consider this to be the most important.
1297
1298 =head1 KNOWN BUGS
1299
1300 If you specify a recurrence which cannot be satisfied for the base date,
1301 or for any time after the base date, the recurrence will crash. This
1302 can only happen if you specify a recurrence that always occurs in the
1303 spring DST transition using the current timezone rules.
1304
1305 For example, in a US timezone, the current timezone rules state that a
1306 DST transition occurs at 02:00:00 on the 2nd Sunday in March and the
1307 clock jumps to 03:00. This started in 2006. As a result, the recurrence
1308
1309 1*3:2:7:2:0:0
1310
1311 with a base date of 2006 or later cannot be satisfied.
1312
1313 =head1 BUGS AND QUESTIONS
1314
1315 Please refer to the L<Date::Manip::Problems> documentation for
1316 information on submitting bug reports or questions to the author.
1317
1318 =head1 SEE ALSO
1319
1320 L<Date::Manip> - main module documentation
1321
1322 =head1 LICENSE
1323
1324 This script is free software; you can redistribute it and/or
1325 modify it under the same terms as Perl itself.
1326
1327 =head1 AUTHOR
1328
1329 Sullivan Beck ([email protected])
1330
1331 =cut