ethiopic-js
Version:
Converts Ethiopian calendar dates into Gregorian and vice versa.
77 lines (59 loc) • 3.74 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 EthiopicCalendar = function (_YearMonthCalendar) {
_inherits(EthiopicCalendar, _YearMonthCalendar);
function EthiopicCalendar(jdn, year, month, day) {
_classCallCheck(this, EthiopicCalendar);
return _possibleConstructorReturn(this, (EthiopicCalendar.__proto__ || Object.getPrototypeOf(EthiopicCalendar)).call(this, jdn, year, month, day));
}
_createClass(EthiopicCalendar, [{
key: "toArray",
value: function toArray() {
return [this.year, this.month, this.day];
}
}], [{
key: "fromJdn",
value: function fromJdn(jdn) {
var year = Math.floor((4 * (jdn - _Constants.ethiopic.EPOCH) + 1463) / 1461);
var month = 1 + Math.floor((jdn - EthiopicCalendar.toJdn(year, 1, 1)) / 30);
var day = jdn + 1 - EthiopicCalendar.toJdn(year, month, 1);
return new EthiopicCalendar(jdn, year, month, day);
}
}, {
key: "toJdn",
value: function toJdn(year, month, day) {
EthiopicCalendar.validate(year, month, day);
return _Constants.ethiopic.EPOCH - 1 + 365 * (year - 1) + Math.floor(year / 4) + 30 * (month - 1) + day;
}
}, {
key: "isLeapYear",
value: function isLeapYear(year) {
return (0, _Astro.mod)(year, 4) === 0;
}
}, {
key: "validate",
value: function validate(year, month, day) {
if (month < 1 || month > 13) {
throw new _BaseCalendars.CalendarValidationException('Invalid month');
}
var maxDaysOfMonth13 = EthiopicCalendar.isLeapYear(year) ? 6 : 5;
if (month === 13 && day > maxDaysOfMonth13) {
throw new _BaseCalendars.CalendarValidationException('Invalid day');
}
if (day < 1 || day > 30) {
throw new _BaseCalendars.CalendarValidationException('Invalid day');
}
}
}]);
return EthiopicCalendar;
}(_BaseCalendars.YearMonthCalendar);
exports.default = EthiopicCalendar;