1 | import { freqIsDailyOrGreater } from '../types';
|
2 | import dateutil from '../dateutil';
|
3 | import Iterinfo from '../iterinfo/index';
|
4 | import RRule from '../rrule';
|
5 | import { buildTimeset } from '../parseoptions';
|
6 | import { notEmpty, includes, isPresent } from '../helpers';
|
7 | import { DateWithZone } from '../datewithzone';
|
8 | import { buildPoslist } from './poslist';
|
9 | import { DateTime } from '../datetime';
|
10 | export function iter(iterResult, options) {
|
11 | var dtstart = options.dtstart, freq = options.freq, interval = options.interval, until = options.until, bysetpos = options.bysetpos;
|
12 | var count = options.count;
|
13 | if (count === 0 || interval === 0) {
|
14 | return emitResult(iterResult);
|
15 | }
|
16 | var counterDate = DateTime.fromDate(dtstart);
|
17 | var ii = new Iterinfo(options);
|
18 | ii.rebuild(counterDate.year, counterDate.month);
|
19 | var timeset = makeTimeset(ii, counterDate, options);
|
20 | while (true) {
|
21 | var _a = ii.getdayset(freq)(counterDate.year, counterDate.month, counterDate.day), dayset = _a[0], start = _a[1], end = _a[2];
|
22 | var filtered = removeFilteredDays(dayset, start, end, ii, options);
|
23 | if (notEmpty(bysetpos)) {
|
24 | var poslist = buildPoslist(bysetpos, timeset, start, end, ii, dayset);
|
25 | for (var j = 0; j < poslist.length; j++) {
|
26 | var res = poslist[j];
|
27 | if (until && res > until) {
|
28 | return emitResult(iterResult);
|
29 | }
|
30 | if (res >= dtstart) {
|
31 | var rezonedDate = rezoneIfNeeded(res, options);
|
32 | if (!iterResult.accept(rezonedDate)) {
|
33 | return emitResult(iterResult);
|
34 | }
|
35 | if (count) {
|
36 | --count;
|
37 | if (!count) {
|
38 | return emitResult(iterResult);
|
39 | }
|
40 | }
|
41 | }
|
42 | }
|
43 | }
|
44 | else {
|
45 | for (var j = start; j < end; j++) {
|
46 | var currentDay = dayset[j];
|
47 | if (!isPresent(currentDay)) {
|
48 | continue;
|
49 | }
|
50 | var date = dateutil.fromOrdinal(ii.yearordinal + currentDay);
|
51 | for (var k = 0; k < timeset.length; k++) {
|
52 | var time = timeset[k];
|
53 | var res = dateutil.combine(date, time);
|
54 | if (until && res > until) {
|
55 | return emitResult(iterResult);
|
56 | }
|
57 | if (res >= dtstart) {
|
58 | var rezonedDate = rezoneIfNeeded(res, options);
|
59 | if (!iterResult.accept(rezonedDate)) {
|
60 | return emitResult(iterResult);
|
61 | }
|
62 | if (count) {
|
63 | --count;
|
64 | if (!count) {
|
65 | return emitResult(iterResult);
|
66 | }
|
67 | }
|
68 | }
|
69 | }
|
70 | }
|
71 | }
|
72 | if (options.interval === 0) {
|
73 | return emitResult(iterResult);
|
74 | }
|
75 |
|
76 | counterDate.add(options, filtered);
|
77 | if (counterDate.year > dateutil.MAXYEAR) {
|
78 | return emitResult(iterResult);
|
79 | }
|
80 | if (!freqIsDailyOrGreater(freq)) {
|
81 | timeset = ii.gettimeset(freq)(counterDate.hour, counterDate.minute, counterDate.second, 0);
|
82 | }
|
83 | ii.rebuild(counterDate.year, counterDate.month);
|
84 | }
|
85 | }
|
86 | function isFiltered(ii, currentDay, options) {
|
87 | var bymonth = options.bymonth, byweekno = options.byweekno, byweekday = options.byweekday, byeaster = options.byeaster, bymonthday = options.bymonthday, bynmonthday = options.bynmonthday, byyearday = options.byyearday;
|
88 | return ((notEmpty(bymonth) && !includes(bymonth, ii.mmask[currentDay])) ||
|
89 | (notEmpty(byweekno) && !ii.wnomask[currentDay]) ||
|
90 | (notEmpty(byweekday) && !includes(byweekday, ii.wdaymask[currentDay])) ||
|
91 | (notEmpty(ii.nwdaymask) && !ii.nwdaymask[currentDay]) ||
|
92 | (byeaster !== null && !includes(ii.eastermask, currentDay)) ||
|
93 | ((notEmpty(bymonthday) || notEmpty(bynmonthday)) &&
|
94 | !includes(bymonthday, ii.mdaymask[currentDay]) &&
|
95 | !includes(bynmonthday, ii.nmdaymask[currentDay])) ||
|
96 | (notEmpty(byyearday) &&
|
97 | ((currentDay < ii.yearlen &&
|
98 | !includes(byyearday, currentDay + 1) &&
|
99 | !includes(byyearday, -ii.yearlen + currentDay)) ||
|
100 | (currentDay >= ii.yearlen &&
|
101 | !includes(byyearday, currentDay + 1 - ii.yearlen) &&
|
102 | !includes(byyearday, -ii.nextyearlen + currentDay - ii.yearlen)))));
|
103 | }
|
104 | function rezoneIfNeeded(date, options) {
|
105 | return new DateWithZone(date, options.tzid).rezonedDate();
|
106 | }
|
107 | function emitResult(iterResult) {
|
108 | return iterResult.getValue();
|
109 | }
|
110 | function removeFilteredDays(dayset, start, end, ii, options) {
|
111 | var filtered = false;
|
112 | for (var dayCounter = start; dayCounter < end; dayCounter++) {
|
113 | var currentDay = dayset[dayCounter];
|
114 | filtered = isFiltered(ii, currentDay, options);
|
115 | if (filtered)
|
116 | dayset[currentDay] = null;
|
117 | }
|
118 | return filtered;
|
119 | }
|
120 | function makeTimeset(ii, counterDate, options) {
|
121 | var freq = options.freq, byhour = options.byhour, byminute = options.byminute, bysecond = options.bysecond;
|
122 | if (freqIsDailyOrGreater(freq)) {
|
123 | return buildTimeset(options);
|
124 | }
|
125 | if ((freq >= RRule.HOURLY &&
|
126 | notEmpty(byhour) &&
|
127 | !includes(byhour, counterDate.hour)) ||
|
128 | (freq >= RRule.MINUTELY &&
|
129 | notEmpty(byminute) &&
|
130 | !includes(byminute, counterDate.minute)) ||
|
131 | (freq >= RRule.SECONDLY &&
|
132 | notEmpty(bysecond) &&
|
133 | !includes(bysecond, counterDate.second))) {
|
134 | return [];
|
135 | }
|
136 | return ii.gettimeset(freq)(counterDate.hour, counterDate.minute, counterDate.second, counterDate.millisecond);
|
137 | }
|
138 |
|
\ | No newline at end of file |