UNPKG

gregorian-ethiopian-datepicker

Version:

A React date picker component supporting both Gregorian and Ethiopian calendars

95 lines (94 loc) 3.24 kB
"use strict"; /** * Ethiopian calendar utility functions */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getCurrentEthiopianDate = exports.getEthiopianMonthName = exports.isValidEthiopianDate = exports.jdnToEthiopian = exports.ethiopianToJDN = exports.getEthiopianMonthDays = exports.isEthiopianLeapYear = void 0; var gregorian_1 = require("./gregorian"); // Ethiopian calendar constants var ETHIOPIAN_EPOCH = 1723856; // Julian Day Number for Meskerem 1, 1 E.E. var ETHIOPIAN_MONTH_DAYS = [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5]; // 13 months var ETHIOPIAN_MONTH_NAMES = [ 'Meskerem', 'Tikimit', 'Hidar', 'Tahesas', 'Tir', 'Yekatit', 'Megabit', 'Miazia', 'Genbot', 'Sene', 'Hamle', 'Nehase', 'Pagume' ]; /** * Checks if a given Ethiopian year is a leap year */ var isEthiopianLeapYear = function (year) { return (year % 4 === 3); }; exports.isEthiopianLeapYear = isEthiopianLeapYear; /** * Gets the number of days in an Ethiopian month */ var getEthiopianMonthDays = function (year, month) { if (month < 1 || month > 13) return 0; if (month === 13) return (0, exports.isEthiopianLeapYear)(year) ? 6 : 5; return ETHIOPIAN_MONTH_DAYS[month - 1]; }; exports.getEthiopianMonthDays = getEthiopianMonthDays; /** * Converts Ethiopian date to Julian Day Number */ var ethiopianToJDN = function (year, month, day) { return ETHIOPIAN_EPOCH + 365 * (year - 1) + Math.floor(year / 4) + 30 * (month - 1) + day - 1; }; exports.ethiopianToJDN = ethiopianToJDN; /** * Converts Julian Day Number to Ethiopian date */ var jdnToEthiopian = function (jdn) { var epoch = ETHIOPIAN_EPOCH; var era = Math.floor((4 * (jdn - epoch)) / 1461); var yoe = Math.floor((jdn - epoch) - 365 * era - Math.floor(era / 4)); var year = era + Math.floor(yoe / 365) + 1; var doy = (jdn - epoch) - 365 * (year - 1) - Math.floor((year - 1) / 4); var month = Math.floor(doy / 30) + 1; var day = (doy % 30) + 1; return [year, month, day]; }; exports.jdnToEthiopian = jdnToEthiopian; /** * Validates an Ethiopian date */ var isValidEthiopianDate = function (year, month, day) { if (year < 1) return false; if (month < 1 || month > 13) return false; if (day < 1 || day > (0, exports.getEthiopianMonthDays)(year, month)) return false; return true; }; exports.isValidEthiopianDate = isValidEthiopianDate; /** * Gets the name of an Ethiopian month */ var getEthiopianMonthName = function (month) { if (month < 1 || month > 13) return ''; return ETHIOPIAN_MONTH_NAMES[month - 1]; }; exports.getEthiopianMonthName = getEthiopianMonthName; /** * Gets today's date in Ethiopian calendar */ var getCurrentEthiopianDate = function () { var today = new Date(); return gregorianToEthiopian(today.getFullYear(), today.getMonth() + 1, today.getDate()); }; exports.getCurrentEthiopianDate = getCurrentEthiopianDate; /** * Helper function to convert Gregorian to Ethiopian (used internally) */ var gregorianToEthiopian = function (gYear, gMonth, gDay) { var jdn = (0, gregorian_1.gregorianToJDN)(gYear, gMonth, gDay); return (0, exports.jdnToEthiopian)(jdn); };