This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
README
1970 lines (1649 loc) · 58.2 KB
/
README
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## This file is in the moin format. The latest version is found
## at https://moin.conectiva.com.br/DateUtil
== Contents ==
[[TableOfContents]]
== Description ==
The '''dateutil''' module provides powerful extensions to
the standard '''datetime''' module, available in Python 2.3+.
== Features ==
* Computing of relative deltas (next month, next year,
next monday, last week of month, etc);
* Computing of relative deltas between two given
date and/or datetime objects;
* Computing of dates based on very flexible recurrence rules,
using a superset of the
[ftp://ftp.rfc-editor.org/in-notes/rfc2445.txt iCalendar]
specification. Parsing of RFC strings is supported as well.
* Generic parsing of dates in almost any string format;
* Timezone (tzinfo) implementations for tzfile(5) format
files (/etc/localtime, /usr/share/zoneinfo, etc), TZ
environment string (in all known formats), iCalendar
format files, given ranges (with help from relative deltas),
local machine timezone, fixed offset timezone, UTC timezone,
and Windows registry-based time zones.
* Internal up-to-date world timezone information based on
Olson's database.
* Computing of Easter Sunday dates for any given year,
using Western, Orthodox or Julian algorithms;
* More than 400 test cases.
== Quick example ==
Here's a snapshot, just to give an idea about the power of the
package. For more examples, look at the documentation below.
Suppose you want to know how much time is left, in
years/months/days/etc, before the next easter happening on a
year with a Friday 13th in August, and you want to get today's
date out of the "date" unix system command. Here is the code:
{{{
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.rrule import *
from dateutil.parser import *
from datetime import *
import commands
import os
now = parse(commands.getoutput("date"))
today = now.date()
year = rrule(YEARLY,bymonth=8,bymonthday=13,byweekday=FR)[0].year
rdelta = relativedelta(easter(year), today)
print "Today is:", today
print "Year with next Aug 13th on a Friday is:", year
print "How far is the Easter of that year:", rdelta
print "And the Easter of that year is:", today+rdelta
}}}
And here's the output:
{{{
Today is: 2003-10-11
Year with next Aug 13th on a Friday is: 2004
How far is the Easter of that year: relativedelta(months=+6)
And the Easter of that year is: 2004-04-11
}}}
{i} Being exactly 6 months ahead was '''really''' a coincidence :)
== Download ==
The following files are available.
* attachment:python-dateutil-1.0.tar.bz2
* attachment:python-dateutil-1.0-1.noarch.rpm
== Author ==
The dateutil module was written by GustavoNiemeyer <[email protected]>.
== Documentation ==
The following modules are available.
=== relativedelta ===
This module offers the '''relativedelta''' type, which is based
on the specification of the excelent work done by M.-A. Lemburg in his
[http://www.egenix.com/files/python/mxDateTime.html mxDateTime]
extension. However, notice that this type '''does not''' implement the
same algorithm as his work. Do not expect it to behave like
{{{mxDateTime}}}'s counterpart.
==== relativedelta type ====
There's two different ways to build a relativedelta instance. The
first one is passing it two {{{date}}}/{{{datetime}}} instances:
{{{
relativedelta(datetime1, datetime2)
}}}
This will build the relative difference between {{{datetime1}}} and
{{{datetime2}}}, so that the following constraint is always true:
{{{
datetime2+relativedelta(datetime1, datetime2) == datetime1
}}}
Notice that instead of {{{datetime}}} instances, you may use
{{{date}}} instances, or a mix of both.
And the other way is to use any of the following keyword arguments:
year, month, day, hour, minute, second, microsecond::
Absolute information.
years, months, weeks, days, hours, minutes, seconds, microseconds::
Relative information, may be negative.
weekday::
One of the weekday instances ({{{MO}}}, {{{TU}}}, etc). These
instances may receive a parameter {{{n}}}, specifying the {{{n}}}th
weekday, which could be positive or negative (like {{{MO(+2)}}} or
{{{MO(-3)}}}. Not specifying it is the same as specifying {{{+1}}}.
You can also use an integer, where {{{0=MO}}}. Notice that,
for example, if the calculated date is already Monday, using
{{{MO}}} or {{{MO(+1)}}} (which is the same thing in this context),
won't change the day.
leapdays::
Will add given days to the date found, but only if the computed
year is a leap year and the computed date is post 28 of february.
yearday, nlyearday::
Set the yearday or the non-leap year day (jump leap days).
These are converted to {{{day}}}/{{{month}}}/{{{leapdays}}}
information.
==== Behavior of operations ====
If you're curious about exactly how the relative delta will act
on operations, here is a description of its behavior.
1. Calculate the absolute year, using the {{{year}}} argument, or the
original datetime year, if the argument is not present.
1. Add the relative {{{years}}} argument to the absolute year.
1. Do steps 1 and 2 for {{{month}}}/{{{months}}}.
1. Calculate the absolute day, using the {{{day}}} argument, or the
original datetime day, if the argument is not present. Then, subtract
from the day until it fits in the year and month found after their
operations.
1. Add the relative {{{days}}} argument to the absolute day. Notice
that the {{{weeks}}} argument is multiplied by 7 and added to {{{days}}}.
1. If {{{leapdays}}} is present, the computed year is a leap year, and
the computed month is after february, remove one day from the found date.
1. Do steps 1 and 2 for {{{hour}}}/{{{hours}}}, {{{minute}}}/{{{minutes}}},
{{{second}}}/{{{seconds}}}, {{{microsecond}}}/{{{microseconds}}}.
1. If the {{{weekday}}} argument is present, calculate the {{{n}}}th
occurrence of the given weekday.
==== Examples ====
Let's begin our trip.
{{{
>>> from datetime import *; from dateutil.relativedelta import *
>>> import calendar
}}}
Store some values.
{{{
>>> NOW = datetime.now()
>>> TODAY = date.today()
>>> NOW
datetime.datetime(2003, 9, 17, 20, 54, 47, 282310)
>>> TODAY
datetime.date(2003, 9, 17)
}}}
Next month.
{{{
>>> NOW+relativedelta(months=+1)
datetime.datetime(2003, 10, 17, 20, 54, 47, 282310)
}}}
Next month, plus one week.
{{{
>>> NOW+relativedelta(months=+1, weeks=+1)
datetime.datetime(2003, 10, 24, 20, 54, 47, 282310)
}}}
Next month, plus one week, at 10am.
{{{
>>> TODAY+relativedelta(months=+1, weeks=+1, hour=10)
datetime.datetime(2003, 10, 24, 10, 0)
}}}
Let's try the other way around. Notice that the
hour setting we get in the relativedelta is relative,
since it's a difference, and the weeks parameter
has gone.
{{{
>>> relativedelta(datetime(2003, 10, 24, 10, 0), TODAY)
relativedelta(months=+1, days=+7, hours=+10)
}}}
One month before one year.
{{{
>>> NOW+relativedelta(years=+1, months=-1)
datetime.datetime(2004, 8, 17, 20, 54, 47, 282310)
}}}
How does it handle months with different numbers of days?
Notice that adding one month will never cross the month
boundary.
{{{
>>> date(2003,1,27)+relativedelta(months=+1)
datetime.date(2003, 2, 27)
>>> date(2003,1,31)+relativedelta(months=+1)
datetime.date(2003, 2, 28)
>>> date(2003,1,31)+relativedelta(months=+2)
datetime.date(2003, 3, 31)
}}}
The logic for years is the same, even on leap years.
{{{
>>> date(2000,2,28)+relativedelta(years=+1)
datetime.date(2001, 2, 28)
>>> date(2000,2,29)+relativedelta(years=+1)
datetime.date(2001, 2, 28)
>>> date(1999,2,28)+relativedelta(years=+1)
datetime.date(2000, 2, 28)
>>> date(1999,3,1)+relativedelta(years=+1)
datetime.date(2000, 3, 1)
>>> date(2001,2,28)+relativedelta(years=-1)
datetime.date(2000, 2, 28)
>>> date(2001,3,1)+relativedelta(years=-1)
datetime.date(2000, 3, 1)
}}}
Next friday.
{{{
>>> TODAY+relativedelta(weekday=FR)
datetime.date(2003, 9, 19)
>>> TODAY+relativedelta(weekday=calendar.FRIDAY)
datetime.date(2003, 9, 19)
}}}
Last friday in this month.
{{{
>>> TODAY+relativedelta(day=31, weekday=FR(-1))
datetime.date(2003, 9, 26)
}}}
Next wednesday (it's today!).
{{{
>>> TODAY+relativedelta(weekday=WE(+1))
datetime.date(2003, 9, 17)
}}}
Next wednesday, but not today.
{{{
>>> TODAY+relativedelta(days=+1, weekday=WE(+1))
datetime.date(2003, 9, 24)
}}}
Following
[http://www.cl.cam.ac.uk/~mgk25/iso-time.html ISO year week number notation]
find the first day of the 15th week of 1997.
{{{
>>> datetime(1997,1,1)+relativedelta(day=4, weekday=MO(-1), weeks=+14)
datetime.datetime(1997, 4, 7, 0, 0)
}}}
How long ago has the millennium changed?
{{{
>>> relativedelta(NOW, date(2001,1,1))
relativedelta(years=+2, months=+8, days=+16,
hours=+20, minutes=+54, seconds=+47, microseconds=+282310)
}}}
How old is John?
{{{
>>> johnbirthday = datetime(1978, 4, 5, 12, 0)
>>> relativedelta(NOW, johnbirthday)
relativedelta(years=+25, months=+5, days=+12,
hours=+8, minutes=+54, seconds=+47, microseconds=+282310)
}}}
It works with dates too.
{{{
>>> relativedelta(TODAY, johnbirthday)
relativedelta(years=+25, months=+5, days=+11, hours=+12)
}}}
Obtain today's date using the yearday:
{{{
>>> date(2003, 1, 1)+relativedelta(yearday=260)
datetime.date(2003, 9, 17)
}}}
We can use today's date, since yearday should be absolute
in the given year:
{{{
>>> TODAY+relativedelta(yearday=260)
datetime.date(2003, 9, 17)
}}}
Last year it should be in the same day:
{{{
>>> date(2002, 1, 1)+relativedelta(yearday=260)
datetime.date(2002, 9, 17)
}}}
But not in a leap year:
{{{
>>> date(2000, 1, 1)+relativedelta(yearday=260)
datetime.date(2000, 9, 16)
}}}
We can use the non-leap year day to ignore this:
{{{
>>> date(2000, 1, 1)+relativedelta(nlyearday=260)
datetime.date(2000, 9, 17)
}}}
=== rrule ===
The rrule module offers a small, complete, and very fast, implementation
of the recurrence rules documented in the
[ftp://ftp.rfc-editor.org/in-notes/rfc2445.txt iCalendar RFC], including
support for caching of results.
==== rrule type ====
That's the base of the rrule operation. It accepts all the keywords
defined in the RFC as its constructor parameters (except {{{byday}}},
which was renamed to {{{byweekday}}}) and more. The constructor
prototype is:
{{{
rrule(freq)
}}}
Where {{{freq}}} must be one of {{{YEARLY}}}, {{{MONTHLY}}},
{{{WEEKLY}}}, {{{DAILY}}}, {{{HOURLY}}}, {{{MINUTELY}}},
or {{{SECONDLY}}}.
Additionally, it supports the following keyword arguments:
cache::
If given, it must be a boolean value specifying to enable
or disable caching of results. If you will use the same
{{{rrule}}} instance multiple times, enabling caching will
improve the performance considerably.
dtstart::
The recurrence start. Besides being the base for the
recurrence, missing parameters in the final recurrence
instances will also be extracted from this date. If not
given, {{{datetime.now()}}} will be used instead.
interval::
The interval between each {{{freq}}} iteration. For example,
when using {{{YEARLY}}}, an interval of {{{2}}} means
once every two years, but with {{{HOURLY}}}, it means
once every two hours. The default interval is {{{1}}}.
wkst::
The week start day. Must be one of the {{{MO}}}, {{{TU}}},
{{{WE}}} constants, or an integer, specifying the first day
of the week. This will affect recurrences based on weekly
periods. The default week start is got from
{{{calendar.firstweekday()}}}, and may be modified by
{{{calendar.setfirstweekday()}}}.
count::
How many occurrences will be generated.
until::
If given, this must be a {{{datetime}}} instance, that will
specify the limit of the recurrence. If a recurrence instance
happens to be the same as the {{{datetime}}} instance given
in the {{{until}}} keyword, this will be the last occurrence.
bysetpos::
If given, it must be either an integer, or a sequence of
integers, positive or negative. Each given integer will
specify an occurrence number, corresponding to the nth
occurrence of the rule inside the frequency period. For
example, a {{{bysetpos}}} of {{{-1}}} if combined with a
{{{MONTHLY}}} frequency, and a {{{byweekday}}} of
{{{(MO, TU, WE, TH, FR)}}}, will result in the last work
day of every month.
bymonth::
If given, it must be either an integer, or a sequence of
integers, meaning the months to apply the recurrence to.
bymonthday::
If given, it must be either an integer, or a sequence of
integers, meaning the month days to apply the recurrence to.
byyearday::
If given, it must be either an integer, or a sequence of
integers, meaning the year days to apply the recurrence to.
byweekno::
If given, it must be either an integer, or a sequence of
integers, meaning the week numbers to apply the recurrence
to. Week numbers have the meaning described in ISO8601,
that is, the first week of the year is that containing at
least four days of the new year.
byweekday::
If given, it must be either an integer ({{{0 == MO}}}), a
sequence of integers, one of the weekday constants
({{{MO}}}, {{{TU}}}, etc), or a sequence of these constants.
When given, these variables will define the weekdays where
the recurrence will be applied. It's also possible to use
an argument {{{n}}} for the weekday instances, which will
mean the {{{n}}}''th'' occurrence of this weekday in the
period. For example, with {{{MONTHLY}}}, or with
{{{YEARLY}}} and {{{BYMONTH}}}, using {{{FR(+1)}}}
in {{{byweekday}}} will specify the first friday of the
month where the recurrence happens. Notice that in the RFC
documentation, this is specified as {{{BYDAY}}}, but was
renamed to avoid the ambiguity of that keyword.
byhour::
If given, it must be either an integer, or a sequence of
integers, meaning the hours to apply the recurrence to.
byminute::
If given, it must be either an integer, or a sequence of
integers, meaning the minutes to apply the recurrence to.
bysecond::
If given, it must be either an integer, or a sequence of
integers, meaning the seconds to apply the recurrence to.
byeaster::
If given, it must be either an integer, or a sequence of
integers, positive or negative. Each integer will define
an offset from the Easter Sunday. Passing the offset
{{{0}}} to {{{byeaster}}} will yield the Easter Sunday
itself. This is an extension to the RFC specification.
==== rrule methods ====
The following methods are available in {{{rrule}}} instances:
rrule.before(dt, inc=False)::
Returns the last recurrence before the given {{{datetime}}}
instance. The {{{inc}}} keyword defines what happens if
{{{dt}}} '''is''' an occurrence. With {{{inc == True}}},
if {{{dt}}} itself is an occurrence, it will be returned.
rrule.after(dt, inc=False)::
Returns the first recurrence after the given {{{datetime}}}
instance. The {{{inc}}} keyword defines what happens if
{{{dt}}} '''is''' an occurrence. With {{{inc == True}}},
if {{{dt}}} itself is an occurrence, it will be returned.
rrule.between(after, before, inc=False)::
Returns all the occurrences of the rrule between {{{after}}}
and {{{before}}}. The {{{inc}}} keyword defines what happens
if {{{after}}} and/or {{{before}}} are themselves occurrences.
With {{{inc == True}}}, they will be included in the list,
if they are found in the recurrence set.
rrule.count()::
Returns the number of recurrences in this set. It will have
go trough the whole recurrence, if this hasn't been done
before.
Besides these methods, {{{rrule}}} instances also support
the {{{__getitem__()}}} and {{{__contains__()}}} special methods,
meaning that these are valid expressions:
{{{
rr = rrule(...)
if datetime(...) in rr:
...
print rr[0]
print rr[-1]
print rr[1:2]
print rr[::-2]
}}}
The getitem/slicing mechanism is smart enough to avoid getting the whole
recurrence set, if possible.
==== Notes ====
* The rrule type has no {{{byday}}} keyword. The equivalent keyword
has been replaced by the {{{byweekday}}} keyword, to remove the
ambiguity present in the original keyword.
* Unlike documented in the RFC, the starting datetime ({{{dtstart}}})
is not the first recurrence instance, unless it does fit in the
specified rules. In a python module context, this behavior makes more
sense than otherwise. Notice that you can easily get the original
behavior by using a rruleset and adding the {{{dtstart}}} as an
{{{rdate}}} recurrence.
* Unlike documented in the RFC, every keyword is valid on every
frequency (the RFC documents that {{{byweekno}}} is only valid
on yearly frequencies, for example).
* In addition to the documented keywords, a {{{byeaster}}} keyword
was introduced, making it easy to compute recurrent events relative
to the Easter Sunday.
==== rrule examples ====
These examples were converted from the RFC.
Prepare the environment.
{{{
>>> from dateutil.rrule import *
>>> from dateutil.parser import *
>>> from datetime import *
>>> import pprint
>>> import sys
>>> sys.displayhook = pprint.pprint
}}}
Daily, for 10 occurrences.
{{{
>>> list(rrule(DAILY, count=10,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 3, 9, 0),
datetime.datetime(1997, 9, 4, 9, 0),
datetime.datetime(1997, 9, 5, 9, 0),
datetime.datetime(1997, 9, 6, 9, 0),
datetime.datetime(1997, 9, 7, 9, 0),
datetime.datetime(1997, 9, 8, 9, 0),
datetime.datetime(1997, 9, 9, 9, 0),
datetime.datetime(1997, 9, 10, 9, 0),
datetime.datetime(1997, 9, 11, 9, 0)]
}}}
Daily until December 24, 1997
{{{
>>> list(rrule(DAILY,
dtstart=parse("19970902T090000"),
until=parse("19971224T000000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 3, 9, 0),
datetime.datetime(1997, 9, 4, 9, 0),
(...)
datetime.datetime(1997, 12, 21, 9, 0),
datetime.datetime(1997, 12, 22, 9, 0),
datetime.datetime(1997, 12, 23, 9, 0)]
}}}
Every other day, 5 occurrences.
{{{
>>> list(rrule(DAILY, interval=2, count=5,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 4, 9, 0),
datetime.datetime(1997, 9, 6, 9, 0),
datetime.datetime(1997, 9, 8, 9, 0),
datetime.datetime(1997, 9, 10, 9, 0)]
}}}
Every 10 days, 5 occurrences.
{{{
>>> list(rrule(DAILY, interval=10, count=5,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 12, 9, 0),
datetime.datetime(1997, 9, 22, 9, 0),
datetime.datetime(1997, 10, 2, 9, 0),
datetime.datetime(1997, 10, 12, 9, 0)]
}}}
Everyday in January, for 3 years.
{{{
>>> list(rrule(YEARLY, bymonth=1, byweekday=range(7),
dtstart=parse("19980101T090000"),
until=parse("20000131T090000")))
[datetime.datetime(1998, 1, 1, 9, 0),
datetime.datetime(1998, 1, 2, 9, 0),
(...)
datetime.datetime(1998, 1, 30, 9, 0),
datetime.datetime(1998, 1, 31, 9, 0),
datetime.datetime(1999, 1, 1, 9, 0),
datetime.datetime(1999, 1, 2, 9, 0),
(...)
datetime.datetime(1999, 1, 30, 9, 0),
datetime.datetime(1999, 1, 31, 9, 0),
datetime.datetime(2000, 1, 1, 9, 0),
datetime.datetime(2000, 1, 2, 9, 0),
(...)
datetime.datetime(2000, 1, 29, 9, 0),
datetime.datetime(2000, 1, 31, 9, 0)]
}}}
Same thing, in another way.
{{{
>>> list(rrule(DAILY, bymonth=1,
dtstart=parse("19980101T090000"),
until=parse("20000131T090000")))
(...)
}}}
Weekly for 10 occurrences.
{{{
>>> list(rrule(WEEKLY, count=10,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 9, 9, 0),
datetime.datetime(1997, 9, 16, 9, 0),
datetime.datetime(1997, 9, 23, 9, 0),
datetime.datetime(1997, 9, 30, 9, 0),
datetime.datetime(1997, 10, 7, 9, 0),
datetime.datetime(1997, 10, 14, 9, 0),
datetime.datetime(1997, 10, 21, 9, 0),
datetime.datetime(1997, 10, 28, 9, 0),
datetime.datetime(1997, 11, 4, 9, 0)]
}}}
Every other week, 6 occurrences.
{{{
>>> list(rrule(WEEKLY, interval=2, count=6,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 16, 9, 0),
datetime.datetime(1997, 9, 30, 9, 0),
datetime.datetime(1997, 10, 14, 9, 0),
datetime.datetime(1997, 10, 28, 9, 0),
datetime.datetime(1997, 11, 11, 9, 0)]
}}}
Weekly on Tuesday and Thursday for 5 weeks.
{{{
>>> list(rrule(WEEKLY, count=10, wkst=SU, byweekday=(TU,TH),
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 4, 9, 0),
datetime.datetime(1997, 9, 9, 9, 0),
datetime.datetime(1997, 9, 11, 9, 0),
datetime.datetime(1997, 9, 16, 9, 0),
datetime.datetime(1997, 9, 18, 9, 0),
datetime.datetime(1997, 9, 23, 9, 0),
datetime.datetime(1997, 9, 25, 9, 0),
datetime.datetime(1997, 9, 30, 9, 0),
datetime.datetime(1997, 10, 2, 9, 0)]
}}}
Every other week on Tuesday and Thursday, for 8 occurrences.
{{{
>>> list(rrule(WEEKLY, interval=2, count=8,
wkst=SU, byweekday=(TU,TH),
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 4, 9, 0),
datetime.datetime(1997, 9, 16, 9, 0),
datetime.datetime(1997, 9, 18, 9, 0),
datetime.datetime(1997, 9, 30, 9, 0),
datetime.datetime(1997, 10, 2, 9, 0),
datetime.datetime(1997, 10, 14, 9, 0),
datetime.datetime(1997, 10, 16, 9, 0)]
}}}
Monthly on the 1st Friday for ten occurrences.
{{{
>>> list(rrule(MONTHLY, count=10, byweekday=FR(1),
dtstart=parse("19970905T090000")))
[datetime.datetime(1997, 9, 5, 9, 0),
datetime.datetime(1997, 10, 3, 9, 0),
datetime.datetime(1997, 11, 7, 9, 0),
datetime.datetime(1997, 12, 5, 9, 0),
datetime.datetime(1998, 1, 2, 9, 0),
datetime.datetime(1998, 2, 6, 9, 0),
datetime.datetime(1998, 3, 6, 9, 0),
datetime.datetime(1998, 4, 3, 9, 0),
datetime.datetime(1998, 5, 1, 9, 0),
datetime.datetime(1998, 6, 5, 9, 0)]
}}}
Every other month on the 1st and last Sunday of the month for 10 occurrences.
{{{
>>> list(rrule(MONTHLY, interval=2, count=10,
byweekday=(SU(1), SU(-1)),
dtstart=parse("19970907T090000")))
[datetime.datetime(1997, 9, 7, 9, 0),
datetime.datetime(1997, 9, 28, 9, 0),
datetime.datetime(1997, 11, 2, 9, 0),
datetime.datetime(1997, 11, 30, 9, 0),
datetime.datetime(1998, 1, 4, 9, 0),
datetime.datetime(1998, 1, 25, 9, 0),
datetime.datetime(1998, 3, 1, 9, 0),
datetime.datetime(1998, 3, 29, 9, 0),
datetime.datetime(1998, 5, 3, 9, 0),
datetime.datetime(1998, 5, 31, 9, 0)]
}}}
Monthly on the second to last Monday of the month for 6 months.
{{{
>>> list(rrule(MONTHLY, count=6, byweekday=MO(-2),
dtstart=parse("19970922T090000")))
[datetime.datetime(1997, 9, 22, 9, 0),
datetime.datetime(1997, 10, 20, 9, 0),
datetime.datetime(1997, 11, 17, 9, 0),
datetime.datetime(1997, 12, 22, 9, 0),
datetime.datetime(1998, 1, 19, 9, 0),
datetime.datetime(1998, 2, 16, 9, 0)]
}}}
Monthly on the third to the last day of the month, for 6 months.
{{{
>>> list(rrule(MONTHLY, count=6, bymonthday=-3,
dtstart=parse("19970928T090000")))
[datetime.datetime(1997, 9, 28, 9, 0),
datetime.datetime(1997, 10, 29, 9, 0),
datetime.datetime(1997, 11, 28, 9, 0),
datetime.datetime(1997, 12, 29, 9, 0),
datetime.datetime(1998, 1, 29, 9, 0),
datetime.datetime(1998, 2, 26, 9, 0)]
}}}
Monthly on the 2nd and 15th of the month for 5 occurrences.
{{{
>>> list(rrule(MONTHLY, count=5, bymonthday=(2,15),
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 15, 9, 0),
datetime.datetime(1997, 10, 2, 9, 0),
datetime.datetime(1997, 10, 15, 9, 0),
datetime.datetime(1997, 11, 2, 9, 0)]
}}}
Monthly on the first and last day of the month for 3 occurrences.
{{{
>>> list(rrule(MONTHLY, count=5, bymonthday=(-1,1,),
dtstart=parse("1997090
2T090000")))
[datetime.datetime(1997, 9, 30, 9, 0),
datetime.datetime(1997, 10, 1, 9, 0),
datetime.datetime(1997, 10, 31, 9, 0),
datetime.datetime(1997, 11, 1, 9, 0),
datetime.datetime(1997, 11, 30, 9, 0)]
}}}
Every 18 months on the 10th thru 15th of the month for 10 occurrences.
{{{
>>> list(rrule(MONTHLY, interval=18, count=10,
bymonthday=range(10,16),
dtstart=parse("19970910T090000")))
[datetime.datetime(1997, 9, 10, 9, 0),
datetime.datetime(1997, 9, 11, 9, 0),
datetime.datetime(1997, 9, 12, 9, 0),
datetime.datetime(1997, 9, 13, 9, 0),
datetime.datetime(1997, 9, 14, 9, 0),
datetime.datetime(1997, 9, 15, 9, 0),
datetime.datetime(1999, 3, 10, 9, 0),
datetime.datetime(1999, 3, 11, 9, 0),
datetime.datetime(1999, 3, 12, 9, 0),
datetime.datetime(1999, 3, 13, 9, 0)]
}}}
Every Tuesday, every other month, 6 occurences.
{{{
>>> list(rrule(MONTHLY, interval=2, count=6, byweekday=TU,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 9, 9, 0),
datetime.datetime(1997, 9, 16, 9, 0),
datetime.datetime(1997, 9, 23, 9, 0),
datetime.datetime(1997, 9, 30, 9, 0),
datetime.datetime(1997, 11, 4, 9, 0)]
}}}
Yearly in June and July for 10 occurrences.
{{{
>>> list(rrule(YEARLY, count=4, bymonth=(6,7),
dtstart=parse("19970610T0900
00")))
[datetime.datetime(1997, 6, 10, 9, 0),
datetime.datetime(1997, 7, 10, 9, 0),
datetime.datetime(1998, 6, 10, 9, 0),
datetime.datetime(1998, 7, 10, 9, 0)]
}}}
Every 3rd year on the 1st, 100th and 200th day for 4 occurrences.
{{{
>>> list(rrule(YEARLY, count=4, interval=3, byyearday=(1,100,200),
dtstart=parse("19970101T090000")))
[datetime.datetime(1997, 1, 1, 9, 0),
datetime.datetime(1997, 4, 10, 9, 0),
datetime.datetime(1997, 7, 19, 9, 0),
datetime.datetime(2000, 1, 1, 9, 0)]
}}}
Every 20th Monday of the year, 3 occurrences.
{{{
>>> list(rrule(YEARLY, count=3, byweekday=MO(20),
dtstart=parse("19970519T090000")))
[datetime.datetime(1997, 5, 19, 9, 0),
datetime.datetime(1998, 5, 18, 9, 0),
datetime.datetime(1999, 5, 17, 9, 0)]
}}}
Monday of week number 20 (where the default start of the week is Monday),
3 occurrences.
{{{
>>> list(rrule(YEARLY, count=3, byweekno=20, byweekday=MO,
dtstart=parse("19970512T090000")))
[datetime.datetime(1997, 5, 12, 9, 0),
datetime.datetime(1998, 5, 11, 9, 0),
datetime.datetime(1999, 5, 17, 9, 0)]
}}}
The week number 1 may be in the last year.
{{{
>>> list(rrule(WEEKLY, count=3, byweekno=1, byweekday=MO,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 12, 29, 9, 0),
datetime.datetime(1999, 1, 4, 9, 0),
datetime.datetime(2000, 1, 3, 9, 0)]
}}}
And the week numbers greater than 51 may be in the next year.
{{{
>>> list(rrule(WEEKLY, count=3, byweekno=52, byweekday=SU,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 12, 28, 9, 0),
datetime.datetime(1998, 12, 27, 9, 0),
datetime.datetime(2000, 1, 2, 9, 0)]
}}}
Only some years have week number 53:
{{{
>>> list(rrule(WEEKLY, count=3, byweekno=53, byweekday=MO,
dtstart=parse("19970902T090000")))
[datetime.datetime(1998, 12, 28, 9, 0),
datetime.datetime(2004, 12, 27, 9, 0),
datetime.datetime(2009, 12, 28, 9, 0)]
}}}
Every Friday the 13th, 4 occurrences.
{{{
>>> list(rrule(YEARLY, count=4, byweekday=FR, bymonthday=13,
dtstart=parse("19970902T090000")))
[datetime.datetime(1998, 2, 13, 9, 0),
datetime.datetime(1998, 3, 13, 9, 0),
datetime.datetime(1998, 11, 13, 9, 0),
datetime.datetime(1999, 8, 13, 9, 0)]
}}}
Every four years, the first Tuesday after a Monday in November,
3 occurrences (U.S. Presidential Election day):
{{{
>>> list(rrule(YEARLY, interval=4, count=3, bymonth=11,
byweekday=TU, bymonthday=(2,3,4,5,6,7,8),
dtstart=parse("19961105T090000")))
[datetime.datetime(1996, 11, 5, 9, 0),
datetime.datetime(2000, 11, 7, 9, 0),
datetime.datetime(2004, 11, 2, 9, 0)]
}}}
The 3rd instance into the month of one of Tuesday, Wednesday or
Thursday, for the next 3 months:
{{{
>>> list(rrule(MONTHLY, count=3, byweekday=(TU,WE,TH),
bysetpos=3, dtstart=parse("19970904T090000")))
[datetime.datetime(1997, 9, 4, 9, 0),
datetime.datetime(1997, 10, 7, 9, 0),
datetime.datetime(1997, 11, 6, 9, 0)]
}}}
The 2nd to last weekday of the month, 3 occurrences.
{{{
>>> list(rrule(MONTHLY, count=3, byweekday=(MO,TU,WE,TH,FR),
bysetpos=-2, dtstart=parse("19970929T090000")))
[datetime.datetime(1997, 9, 29, 9, 0),
datetime.datetime(1997, 10, 30, 9, 0),
datetime.datetime(1997, 11, 27, 9, 0)]
}}}
Every 3 hours from 9:00 AM to 5:00 PM on a specific day.
{{{
>>> list(rrule(HOURLY, interval=3,
dtstart=parse("19970902T090000"),
until=parse("19970902T170000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 2, 12, 0),
datetime.datetime(1997, 9, 2, 15, 0)]
}}}
Every 15 minutes for 6 occurrences.
{{{
>>> list(rrule(MINUTELY, interval=15, count=6,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 2, 9, 15),
datetime.datetime(1997, 9, 2, 9, 30),
datetime.datetime(1997, 9, 2, 9, 45),
datetime.datetime(1997, 9, 2, 10, 0),
datetime.datetime(1997, 9, 2, 10, 15)]
}}}
Every hour and a half for 4 occurrences.
{{{
>>> list(rrule(MINUTELY, interval=90, count=4,
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 2, 10, 30),
datetime.datetime(1997, 9, 2, 12, 0),
datetime.datetime(1997, 9, 2, 13, 30)]
}}}
Every 20 minutes from 9:00 AM to 4:40 PM for two days.
{{{
>>> list(rrule(MINUTELY, interval=20, count=48,
byhour=range(9,17), byminute=(0,20,40),
dtstart=parse("19970902T090000")))
[datetime.datetime(1997, 9, 2, 9, 0),
datetime.datetime(1997, 9, 2, 9, 20),
(...)
datetime.datetime(1997, 9, 2, 16, 20),
datetime.datetime(1997, 9, 2, 16, 40),
datetime.datetime(1997, 9, 3, 9, 0),
datetime.datetime(1997, 9, 3, 9, 20),
(...)
datetime.datetime(1997, 9, 3, 16, 20),
datetime.datetime(1997, 9, 3, 16, 40)]
}}}
An example where the days generated makes a difference because of {{{wkst}}}.
{{{
>>> list(rrule(WEEKLY, interval=2, count=4,
byweekday=(TU,SU), wkst=MO,
dtstart=parse("19970805T090000")))
[datetime.datetime(1997, 8, 5, 9, 0),
datetime.datetime(1997, 8, 10, 9, 0),
datetime.datetime(1997, 8, 19, 9, 0),
datetime.datetime(1997, 8, 24, 9, 0)]
>>> list(rrule(WEEKLY, interval=2, count=4,
byweekday=(TU,SU), wkst=SU,
dtstart=parse("19970805T090000")))
[datetime.datetime(1997, 8, 5, 9, 0),
datetime.datetime(1997, 8, 17, 9, 0),
datetime.datetime(1997, 8, 19, 9, 0),
datetime.datetime(1997, 8, 31, 9, 0)]
}}}
==== rruleset type ====
The {{{rruleset}}} type allows more complex recurrence setups, mixing
multiple rules, dates, exclusion rules, and exclusion dates.
The type constructor takes the following keyword arguments:
cache::
If True, caching of results will be enabled, improving performance
of multiple queries considerably.
==== rruleset methods ====
The following methods are available:
rruleset.rrule(rrule)::
Include the given {{{rrule}}} instance in the recurrence set
generation.
rruleset.rdate(dt)::
Include the given {{{datetime}}} instance in the recurrence
set generation.
rruleset.exrule(rrule)::
Include the given {{{rrule}}} instance in the recurrence set
exclusion list. Dates which are part of the given recurrence
rules will not be generated, even if some inclusive {{{rrule}}}
or {{{rdate}}} matches them.
rruleset.exdate(dt)::
Include the given {{{datetime}}} instance in the recurrence set
exclusion list. Dates included that way will not be generated,
even if some inclusive {{{rrule}}} or {{{rdate}}} matches them.
rruleset.before(dt, inc=False)::
Returns the last recurrence before the given {{{datetime}}}
instance. The {{{inc}}} keyword defines what happens if
{{{dt}}} '''is''' an occurrence. With {{{inc == True}}},
if {{{dt}}} itself is an occurrence, it will be returned.
rruleset.after(dt, inc=False)::
Returns the first recurrence after the given {{{datetime}}}
instance. The {{{inc}}} keyword defines what happens if
{{{dt}}} '''is''' an occurrence. With {{{inc == True}}},
if {{{dt}}} itself is an occurrence, it will be returned.
rruleset.between(after, before, inc=False)::
Returns all the occurrences of the rrule between {{{after}}}
and {{{before}}}. The {{{inc}}} keyword defines what happens
if {{{after}}} and/or {{{before}}} are themselves occurrences.
With {{{inc == True}}}, they will be included in the list,
if they are found in the recurrence set.