luxon-hijri
Version:
A Hijri date converter based on the Umm al-Qura calendar system, using Luxon for date manipulations.
27 lines (26 loc) • 1.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toGregorian = void 0;
// toGregorian.ts
const luxon_1 = require("luxon");
const hDates_1 = require("./hDates");
const utils_1 = require("./utils");
function toGregorian(hy, hm, hd) {
// Validate the input Hijri date
if (!(0, utils_1.isValidHijriDate)(hy, hm, hd)) {
throw new Error('Invalid Hijri date');
}
const hijriYearRecord = hDates_1.hDatesTable.find(record => record.hy === hy);
if (hijriYearRecord) {
let totalDaysTillMonthStart = 0;
for (let i = 0; i < hm - 1; i++) {
totalDaysTillMonthStart += (hijriYearRecord.dpm >> i) & 1 ? 30 : 29;
}
const totalDays = totalDaysTillMonthStart + hd - 1;
const startDate = luxon_1.DateTime.local(hijriYearRecord.gy, hijriYearRecord.gm, hijriYearRecord.gd);
const gregorianDate = startDate.plus({ days: totalDays });
return gregorianDate.toJSDate();
}
return null;
}
exports.toGregorian = toGregorian;