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

Add support for getting the dominical letter for a year #169

Merged
merged 2 commits into from
Jun 1, 2015
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
29 changes: 29 additions & 0 deletions build/ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -4821,6 +4821,17 @@ ICAL.TimezoneService = (function() {
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 @@ -5629,6 +5640,24 @@ ICAL.TimezoneService = (function() {
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
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