Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BYEASTER. Uses the Anonymous Gregorian algorithm #25

Merged
merged 2 commits into from
Sep 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/rrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ var RRule = function(options, noCache) {
throw 'Invalid options: ' + invalid.join(', ')
}

if (!RRule.FREQUENCIES[options.freq]) {
if (!RRule.FREQUENCIES[options.freq] && options.byeaster === null) {
throw 'Invalid frequency: ' + String(options.freq)
}

Expand All @@ -491,7 +491,7 @@ var RRule = function(options, noCache) {
this.options = opts = _.extend({}, defaults, options);

if (opts.byeaster !== null) {
throw new Error('byeaster not implemented');
opts.freq = RRule.YEARLY;
}

if (!opts.dtstart) {
Expand Down Expand Up @@ -712,7 +712,6 @@ RRule.DEFAULT_OPTIONS = {
byhour: null,
byminute: null,
bysecond: null,
// not implemented:
byeaster: null
};

Expand Down Expand Up @@ -1097,7 +1096,7 @@ RRule.prototype = {
(plb(byweekno) && !ii.wnomask[i]) ||
(plb(byweekday) && !_.include(byweekday, ii.wdaymask[i])) ||
(plb(ii.nwdaymask) && !ii.nwdaymask[i]) ||
(plb(byeaster) && !ii.eastermask[i]) ||
(byeaster !== null && !_.include(ii.eastermask, i)) ||
(
(plb(bymonthday) || plb(bynmonthday)) &&
!_.include(bymonthday, ii.mdaymask[i]) &&
Expand Down Expand Up @@ -1442,6 +1441,9 @@ RRule.parseString = function(rfcString) {
case 'UNTIL':
options.until = dateutil.untilStringToDate(value);
break;
case 'BYEASTER':
options.byeaster = Number(value);
break;
default:
throw new Error("Unknown RRULE property '" + key + "'");
}
Expand Down Expand Up @@ -1477,6 +1479,28 @@ var Iterinfo = function(rrule) {
this.eastermask = null;
};

Iterinfo.prototype.easter = function(y, offset) {
offset = offset || 0;

var a = y % 19,
b = Math.floor(y / 100),
c = y % 100,
d = Math.floor(b / 4),
e = b % 4,
f = Math.floor((b + 8) / 25),
g = Math.floor((b - f + 1) / 3),
h = Math.floor(19 * a + b - d - g + 15) % 30,
i = Math.floor(c / 4),
k = c % 4,
l = Math.floor(32 + 2 * e + 2 * i - h - k) % 7,
m = Math.floor((a + 11 * h + 22 * l) / 451),
month = Math.floor((h + l - 7 * m + 114) / 31),
day = (h + l - 7 * m + 114) % 31 + 1,
date = Date.UTC(y, month - 1, day + offset),
yearStart = Date.UTC(y, 0, 1);

return [ Math.ceil((date - yearStart) / (1000 * 60 * 60 * 24)) ];
}

Iterinfo.prototype.rebuild = function(year, month) {

Expand Down Expand Up @@ -1607,7 +1631,6 @@ Iterinfo.prototype.rebuild = function(year, month) {
}
}
}

}

if (plb(rr.options.bynweekday)
Expand Down Expand Up @@ -1652,12 +1675,13 @@ Iterinfo.prototype.rebuild = function(year, month) {

}

if (plb(rr.options.byeaster)) {/* not implemented */}

this.lastyear = year;
this.lastmonth = month;
}

if (rr.options.byeaster !== null) {
this.eastermask = this.easter(year, rr.options.byeaster);
}
};

Iterinfo.prototype.ydayset = function(year, month, day) {
Expand Down
11 changes: 4 additions & 7 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,32 +354,29 @@ testRecurring('testYearlyByWeekNoAndWeekDayLast', new RRule({freq: RRule.YEARLY,
datetime(1999, 1, 3, 9, 0),
datetime(2000, 1, 2, 9, 0)]);


/* byeaster not implemented
assertRecurring('testYearlyByEaster', new RRule({freq: RRule.YEARLY,
count: 3,
testRecurring('testYearlyByEaster', new RRule({ count: 3,
byeaster: 0,
dtstart: parse("19970902T090000")}),
[datetime(1998, 4, 12, 9, 0),
datetime(1999, 4, 4, 9, 0),
datetime(2000, 4, 23, 9, 0)])

assertRecurring('testYearlyByEasterPos', new RRule({freq: RRule.YEARLY,
testRecurring('testYearlyByEasterPos', new RRule({freq: RRule.YEARLY,
count: 3,
byeaster: 1,
dtstart: parse("19970902T090000")}),
[datetime(1998, 4, 13, 9, 0),
datetime(1999, 4, 5, 9, 0),
datetime(2000, 4, 24, 9, 0)])

assertRecurring('testYearlyByEasterNeg', new RRule({freq: RRule.YEARLY,
testRecurring('testYearlyByEasterNeg', new RRule({freq: RRule.YEARLY,
count: 3,
byeaster: -1,
dtstart: parse("19970902T090000")}),
[datetime(1998, 4, 11, 9, 0),
datetime(1999, 4, 3, 9, 0),
datetime(2000, 4, 22, 9, 0)])
*/

testRecurring('testYearlyByWeekNoAndWeekDay53', new RRule({freq: RRule.YEARLY,
count:3,
byweekno:53,
Expand Down