UNPKG

@jbouduin/holidays-lib

Version:
129 lines (128 loc) 5.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CalendarHelper = void 0; var configuration_1 = require("../configuration"); var configuration_2 = require("../configuration"); var configuration_3 = require("../configuration"); var CalendarHelper = /** @class */ (function () { // <editor-fold desc='Constructor & C°'> function CalendarHelper() { } // </editor-fold> // <editor-fold desc='ICalendar interface methods'> CalendarHelper.prototype.addDays = function (date, days) { return new Date(date.getTime() + (days * 1000 * 60 * 60 * 24)); }; CalendarHelper.prototype.calculateFixedDate = function (fix, year) { return new Date(Date.UTC(year, fix.month, fix.day)); }; CalendarHelper.prototype.calculateFixedWeekday = function (fix, year) { if (fix.which === configuration_2.Which.LAST) { var lastDayOfMonth = new Date(Date.UTC(year, fix.month + 1, 0)); var dayOfLastDayOfMonth = lastDayOfMonth.getDay(); return this.addDays(lastDayOfMonth, fix.weekday - lastDayOfMonth.getDay()); } else { var firstDayOfMonth = new Date(Date.UTC(year, fix.month, 1)); var dayOfFirstDayOfMonth = firstDayOfMonth.getDay(); return this.addDays(firstDayOfMonth, (dayOfFirstDayOfMonth > fix.weekday ? fix.weekday - dayOfFirstDayOfMonth + 7 : fix.weekday - dayOfFirstDayOfMonth) + fix.which * 7); } }; CalendarHelper.prototype.getEasternSunday = function (chronology, year) { switch (chronology) { case configuration_1.ChronologyType.JULIAN: { // originally this checked on year >= 1583. Took it out. return this.getJulianEasternSunday(year); } case configuration_1.ChronologyType.GREGORIAN: { return this.getGregorianEasterSunday(year); } } }; CalendarHelper.prototype.occurs = function (holiday, year) { var result = (holiday.validFrom === configuration_3.BaseHoliday.undefinedValidFrom || holiday.validFrom <= year) && (holiday.validTo === configuration_3.BaseHoliday.undefinedValidTo || holiday.validTo >= year) && this.isValidForCyle(holiday, year); return result; }; // </editor-fold> // <editor-fold desc='Private methods'> CalendarHelper.prototype.getJulianEasternSunday = function (year) { // source: https://www.staff.science.uu.nl/~gent0113/easter/easter_text2a.htm var a = this.generalizedModulo(year, 19); var goldenNumber = a + 1; var b = this.generalizedModulo(year, 4); var c = this.generalizedModulo(year, 7); var d = this.generalizedModulo((19 * a + 15), 30); if (goldenNumber === 1) { d++; } var e = this.generalizedModulo((2 * b + 4 * c - d + 6), 7); var fmj = 113 + d; // Easter full moon [days after -92 March] var dmj = fmj + e + 1; // Easter Sunday [days after -92 March] var fmmj = Math.floor(fmj / 31); // month Easter full moon [March = 3; April = 4] var fmdj = this.generalizedModulo(fmj, 31) + 1; // day Easter full moon var esmj = Math.floor(dmj / 31); // month Easter Sunday [March = 3; April = 4] var esdj = this.generalizedModulo(dmj, 31) + 1; // day Easter Sunday return this.addDays(new Date(Date.UTC(year, esmj === 3 ? 2 : 3, esdj)), 13); }; CalendarHelper.prototype.generalizedModulo = function (a, b) { // generalized modulo function (m mod n) - also valid for negative values of m return ((a % b) + b) % b; }; CalendarHelper.prototype.getGregorianEasterSunday = function (year) { // original yollyday code var a = year % 19; var b = Math.floor(year / 100); var c = year % 100; var d = b / 4; var e = b % 4; var f = Math.floor((b + 8) / 25); var g = Math.floor((b - f + 1) / 3); var h = (19 * a + b - d - g + 15) % 30; var i = Math.floor(c / 4); var j = c % 4; var k = (32 + 2 * e + 2 * i - h - j) % 7; var l = Math.floor((a + 11 * h + 22 * k) / 451); var x = h + k - 7 * l + 114; var month = Math.floor(x / 31); var day = (x % 31) + 1; return new Date(Date.UTC(year, month === 3 ? 2 : 3, day)); }; CalendarHelper.prototype.isValidForCyle = function (holiday, year) { var cycleYears = 1; switch (holiday.cycle) { case configuration_1.Cycle.EVERY_YEAR: { return true; } case configuration_1.Cycle.ODD_YEARS: { return year % 2 !== 0; } case configuration_1.Cycle.EVEN_YEARS: { return year % 2 === 0; } case configuration_1.Cycle.TWO_YEARS: { cycleYears = 2; break; } case configuration_1.Cycle.FOUR_YEARS: { cycleYears = 4; break; } case configuration_1.Cycle.FIVE_YEARS: { cycleYears = 5; break; } case configuration_1.Cycle.SIX_YEARS: { cycleYears = 6; break; } } return (year - holiday.validFrom) % cycleYears === 0; }; return CalendarHelper; }()); exports.CalendarHelper = CalendarHelper;