diff --git a/lib/ical/time.js b/lib/ical/time.js index 31419c56..72d69932 100644 --- a/lib/ical/time.js +++ b/lib/ical/time.js @@ -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 @@ -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} diff --git a/test/time_test.js b/test/time_test.js index 19da0513..0a611fc1 100644 --- a/test/time_test.js +++ b/test/time_test.js @@ -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() {