ethiopic-js
Version:
Converts Ethiopian calendar dates into Gregorian and vice versa.
109 lines (86 loc) • 5.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _BaseCalendars = require("./BaseCalendars");
var _Constants = require("./Constants");
var _Astro = require("./Astro");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var GregorianCalendar = function (_LeapCalendar) {
_inherits(GregorianCalendar, _LeapCalendar);
function GregorianCalendar(jdn, year, month, day) {
_classCallCheck(this, GregorianCalendar);
return _possibleConstructorReturn(this, (GregorianCalendar.__proto__ || Object.getPrototypeOf(GregorianCalendar)).call(this, jdn, year, month, day, GregorianCalendar.isLeapYear(year)));
}
_createClass(GregorianCalendar, [{
key: "toArray",
value: function toArray() {
return [this.year, this.month, this.day];
}
}], [{
key: "toJdn",
value: function toJdn(year, month, day) {
GregorianCalendar.validate(year, month, day);
var y1 = year - 1;
return _Constants.gregorian.EPOCH - 1 + 365 * y1 + Math.floor(y1 / 4) - Math.floor(y1 / 100) + Math.floor(y1 / 400) + Math.floor((367 * month - 362) / 12 + (month <= 2 ? 0 : GregorianCalendar.isLeapYear(year) ? -1 : -2) + day);
}
}, {
key: "validate",
value: function validate(year, month, day) {
if (month < 1 || month > 12) {
throw new _BaseCalendars.CalendarValidationException('Invalid month');
}
if (day < 1) {
throw new _BaseCalendars.CalendarValidationException('Invalid day');
}
var febDays = GregorianCalendar.isLeapYear(year) ? 29 : 28;
if (month === _Constants.Month.FEBRUARY && day <= febDays) {
return;
}
if (daysInMonth[month - 1] < day) {
throw new _BaseCalendars.CalendarValidationException('Invlaid day');
}
}
}, {
key: "isLeapYear",
value: function isLeapYear(year) {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
}, {
key: "jdnToYear",
value: function jdnToYear(jdn) {
var jd0 = Math.floor(jdn - 0.5) + 0.5;
var depoch = jd0 - _Constants.gregorian.EPOCH;
var quadricent = Math.floor(depoch / 146097);
var dqc = (0, _Astro.mod)(depoch, 146097);
var cent = Math.floor(dqc / 36524);
var dcent = (0, _Astro.mod)(dqc, 36524);
var quad = Math.floor(dcent / 1461);
var dquad = (0, _Astro.mod)(dcent, 1461);
var yindex = Math.floor(dquad / 365);
return quadricent * 400 + cent * 100 + quad * 4 + yindex + (cent !== 4 && yindex !== 4 ? 1 : 0);
}
}, {
key: "fromJdn",
value: function fromJdn(jdn) {
var jd0 = Math.floor(jdn - 0.5) + 0.5;
var year = GregorianCalendar.jdnToYear(jd0);
var yearDay = jd0 - GregorianCalendar.toJdn(year, 1, 1);
var leapAdj = jd0 < GregorianCalendar.toJdn(year, 3, 1) ? 0 : GregorianCalendar.isLeapYear(year) ? 1 : 2;
var month = Math.floor(((yearDay + leapAdj) * 12 + 373) / 367);
var day = jd0 - GregorianCalendar.toJdn(year, month, 1) + 1;
return new GregorianCalendar(jdn, year, month, day);
}
}, {
key: "dateDifference",
value: function dateDifference(date1, date2) {
return date2.getJdn() - date1.getJdn();
}
}]);
return GregorianCalendar;
}(_BaseCalendars.LeapCalendar);
exports.default = GregorianCalendar;