Skip to content

Commit

Permalink
Add support for getting the dominical letter for a year
Browse files Browse the repository at this point in the history
  • Loading branch information
kewisch committed May 31, 2015
1 parent d5d9133 commit 91961a8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/ical/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,17 @@
return this.dayOfYear() - delta;
},

/**
* Get the dominical letter for the current year. Letters range from A - G
* for common years, and AG to GF for leap years.
*
* @param {Number} yr The year to retrieve the letter for
* @return {String} The dominical letter.
*/
getDominicalLetter: function() {
return ICAL.Time.getDominicalLetter(this.year);
},

/**
* Finds the nthWeekDay relative to the current month (not day). The
* returned value is a day relative the month that this month belongs to so
Expand Down Expand Up @@ -1195,6 +1206,24 @@
return t;
};

/**
* Get the dominical letter for the given year. Letters range from A - G for
* common years, and AG to GF for leap years.
*
* @param {Number} yr The year to retrieve the letter for
* @return {String} The dominical letter.
*/
ICAL.Time.getDominicalLetter = function(yr) {
var LTRS = "GFEDCBA";
var dom = (yr + (yr / 4 | 0) + (yr / 400 | 0) - (yr / 100 | 0) - 1) % 7;
var isLeap = ICAL.Time.isLeapYear(yr);
if (isLeap) {
return LTRS[(dom + 6) % 7] + LTRS[dom];
} else {
return LTRS[dom];
}
};

/**
* January 1st, 1970 as an ICAL.Time.
* @type {ICAL.Time}
Expand Down
42 changes: 42 additions & 0 deletions test/time_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,48 @@ suite('icaltime', function() {

});

suite('#getDominicalLetter', function() {
test('instance', function() {
var subject = function(yr) {
return (new ICAL.Time({ year: yr })).getDominicalLetter();
};
assert.equal(subject(1989), "A");
assert.equal(subject(1990), "G");
assert.equal(subject(1991), "F");
assert.equal(subject(1993), "C");
assert.equal(subject(1994), "B");
assert.equal(subject(1997), "E");
assert.equal(subject(1998), "D");

assert.equal(subject(2000), "BA");
assert.equal(subject(2004), "DC");
assert.equal(subject(2008), "FE");
assert.equal(subject(2012), "AG");
assert.equal(subject(2016), "CB");
assert.equal(subject(2020), "ED");
assert.equal(subject(2024), "GF");

});
test('static', function() {
var subject = ICAL.Time.getDominicalLetter;
assert.equal(subject(1989), "A");
assert.equal(subject(1990), "G");
assert.equal(subject(1991), "F");
assert.equal(subject(1993), "C");
assert.equal(subject(1994), "B");
assert.equal(subject(1997), "E");
assert.equal(subject(1998), "D");

assert.equal(subject(2000), "BA");
assert.equal(subject(2004), "DC");
assert.equal(subject(2008), "FE");
assert.equal(subject(2012), "AG");
assert.equal(subject(2016), "CB");
assert.equal(subject(2020), "ED");
assert.equal(subject(2024), "GF");
});
});

suite('#nthWeekDay', function() {
suite('negative', function() {
test('last saturday in Sept 2012 (before current day)', function() {
Expand Down

0 comments on commit 91961a8

Please sign in to comment.