UNPKG

luxon-hijri

Version:

Hijri/Gregorian date conversion and formatting using the Umm al-Qura calendar. Built on Luxon. Supports toHijri, toGregorian, formatHijriDate, and isValidHijriDate.

127 lines (120 loc) 3.78 kB
// src/formatPatterns.ts var formatPatterns = { // Hijri Year iYYYY: "Hijri year (4 digits)", iYY: "Hijri year (2 digits)", // Hijri Month iMM: "Hijri month (2 digits, zero-padded)", iM: "Hijri month (1 or 2 digits without zero-padding)", iMMM: "Hijri month (abbreviated name)", iMMMM: "Hijri month (full name)", // Hijri Day iDD: "Hijri day of the month (2 digits, zero-padded)", iD: "Hijri day of the month (1 or 2 digits without zero-padding)", // Hijri Weekday iE: "Hijri weekday (1 digit)", iEEE: "Hijri weekday (abbreviated name)", iEEEE: "Hijri weekday (full name)", // Hour, Minute, Second // These can remain the same as in Gregorian as they don’t change in Hijri HH: "Hour (2 digits, zero-padded, 24-hour clock)", H: "Hour (1 or 2 digits without zero-padding, 24-hour clock)", hh: "Hour (2 digits, zero-padded, 12-hour clock)", h: "Hour (1 or 2 digits without zero-padding, 12-hour clock)", mm: "Minute (2 digits, zero-padded)", m: "Minute (1 or 2 digits without zero-padding)", ss: "Second (2 digits, zero-padded)", s: "Second (1 or 2 digits without zero-padding)", // AM/PM a: "AM/PM marker", // Other iooo: "Hijri era (abbreviated)", ioooo: "Hijri era (full)", // Timezone z: "Timezone (abbreviated)", zz: "Timezone (medium)", zzz: "Timezone (full)", Z: "Timezone offset from UTC (+HH:MM)", ZZ: "Timezone offset from UTC (condensed)" }; // src/hDates.ts import { hDatesTable } from "hijri-core"; // src/hMonths.ts import { hmLong, hmMedium, hmShort } from "hijri-core"; // src/hWeekdays.ts import { hwLong, hwShort, hwNumeric } from "hijri-core"; // src/toGregorian.ts import { toGregorian as coreToGregorian } from "hijri-core"; function toGregorian(hy, hm, hd, options) { const result = coreToGregorian(hy, hm, hd, options); if (result === null) throw new Error("Invalid Hijri date"); return result; } // src/toHijri.ts import { toHijri } from "hijri-core"; // src/formatHijriDate.ts import { DateTime } from "luxon"; var TOKEN_RE = /iYYYY|iYY|iMMMM|iMMM|iMM|iM|iDD|iD|iEEEE|iEEE|iE|ioooo|iooo|HH|H|hh|h|mm|m|ss|s|a|z{1,3}|ZZ|Z/g; function formatHijriDate(hijriDate, format) { if (hijriDate.hm < 1 || hijriDate.hm > 12) { throw new RangeError(`Hijri month must be 1-12, got ${hijriDate.hm}`); } let _gregDt; function getGregDt() { if (!_gregDt) { const greg = toGregorian(hijriDate.hy, hijriDate.hm, hijriDate.hd); _gregDt = DateTime.fromJSDate(greg, { zone: "UTC" }); } return _gregDt; } return format.replace(TOKEN_RE, (match) => { switch (match) { case "iYYYY": return String(hijriDate.hy).padStart(4, "0"); case "iYY": return String(hijriDate.hy % 100).padStart(2, "0"); case "iMM": return String(hijriDate.hm).padStart(2, "0"); case "iM": return String(hijriDate.hm); case "iMMM": return hmMedium[hijriDate.hm - 1]; case "iMMMM": return hmLong[hijriDate.hm - 1]; case "iDD": return String(hijriDate.hd).padStart(2, "0"); case "iD": return String(hijriDate.hd); case "iE": case "iEEE": case "iEEEE": { const idx = getGregDt().weekday % 7; if (match === "iE") return String(hwNumeric[idx]); if (match === "iEEE") return hwShort[idx]; return hwLong[idx]; } case "iooo": case "ioooo": return "AH"; default: return getGregDt().toFormat(match); } }); } // src/utils.ts import { isValidHijriDate } from "hijri-core"; export { formatHijriDate, formatPatterns, hDatesTable, hmLong, hmMedium, hmShort, hwLong, hwNumeric, hwShort, isValidHijriDate, toGregorian, toHijri }; //# sourceMappingURL=index.mjs.map