UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

119 lines (117 loc) • 4.57 kB
import { dateFromFixed, fixedFromIso, mod } from "./fixedDay.js"; // src/internal/temporalCompat/hebrewArithmetic.ts /** Fixed day (R.D.) of Tishri 1, AM 1: `fixed-from-julian(3761 BCE, October 7)`. */ const HEBREW_EPOCH = -1373427; /** Mean Hebrew year in days, used only to estimate the year before correcting it. */ const MEAN_YEAR_DAYS = 35975351 / 98496; /** The estimate is off by at most one year; a few extra steps cost nothing. */ const MAX_YEAR_ADJUSTMENTS = 4; function isHebrewLeapYear(year) { return mod(7 * year + 1, 19) < 7; } /** Months from Tishri AM 1 to Tishri of `year` (`hebrew-calendar-elapsed-days`' month count). */ function monthsBeforeYear(year) { return Math.floor((235 * year - 234) / 19); } function elapsedDays(year) { const monthsElapsed = monthsBeforeYear(year); const partsElapsed = 12084 + 13753 * monthsElapsed; const days = 29 * monthsElapsed + Math.floor(partsElapsed / 25920); return mod(3 * (days + 1), 7) < 3 ? days + 1 : days; } function yearLengthCorrection(year) { const previous = elapsedDays(year - 1); const current = elapsedDays(year); if (elapsedDays(year + 1) - current === 356) return 2; return current - previous === 382 ? 1 : 0; } /** Fixed day (R.D.) of Tishri 1 of `year`. */ function hebrewNewYear(year) { return HEBREW_EPOCH + elapsedDays(year) + yearLengthCorrection(year); } /** Month codes and lengths from Tishri, per `last-day-of-hebrew-month`. */ function monthsOfYear(year) { const yearLength = hebrewNewYear(year + 1) - hebrewNewYear(year); const longHeshvan = yearLength === 355 || yearLength === 385; const shortKislev = yearLength === 353 || yearLength === 383; const adar = isHebrewLeapYear(year) ? [["M05L", 30], ["M06", 29]] : [["M06", 29]]; return [ ["M01", 30], ["M02", longHeshvan ? 30 : 29], ["M03", shortKislev ? 29 : 30], ["M04", 29], ["M05", 30], ...adar, ["M07", 30], ["M08", 29], ["M09", 30], ["M10", 29], ["M11", 30], ["M12", 29] ]; } function fieldsInYear(year, dayOfYear) { let remaining = dayOfYear; const months = monthsOfYear(year); for (const [index, [monthCode, length]] of months.entries()) { if (remaining < length) return { year, month: index + 1, monthCode, day: remaining + 1 }; remaining -= length; } throw new RangeError(`Day ${dayOfYear} is past Hebrew year ${year}`); } /** The Hebrew year, ordinal month, month code and day of a fixed day (R.D.). */ function hebrewFieldsFromFixed(fixed) { let year = Math.floor((fixed - HEBREW_EPOCH) / MEAN_YEAR_DAYS) + 1; for (let step = 0; step < MAX_YEAR_ADJUSTMENTS; step++) { const newYear = hebrewNewYear(year); if (newYear > fixed) year -= 1; else if (hebrewNewYear(year + 1) <= fixed) year += 1; else return fieldsInYear(year, fixed - newYear); } throw new RangeError(`Hebrew year of fixed day ${fixed} did not converge`); } function monthIndex({ year, month }) { return monthsBeforeYear(year) + month - 1; } function yearMonthAt(index) { let year = Math.floor(19 * index / 235) + 1; for (let step = 0; step < MAX_YEAR_ADJUSTMENTS; step++) if (monthsBeforeYear(year) > index) year -= 1; else if (monthsBeforeYear(year + 1) <= index) year += 1; else return { year, month: index - monthsBeforeYear(year) + 1 }; throw new RangeError(`Hebrew year of month ${index} did not converge`); } /** * The Hebrew calendar as an integer arithmetic model for `nonIsoArithmetic.ts`, so add and until * are correct for years <= 0 too (D3 + D4). Every answer is this file's arithmetic; nothing is * read from the runtime. */ const hebrewArithmeticModel = { calendarId: "hebrew", fields(date) { const iso = date.withCalendar("iso8601"); return hebrewFieldsFromFixed(fixedFromIso(iso.year, iso.month, iso.day)); }, monthOrdinal(year, monthCode) { const index = monthsOfYear(year).findIndex(([code]) => code === monthCode); return index < 0 ? "missing" : index + 1; }, addMonths: (start, months) => yearMonthAt(monthIndex(start) + months), monthsBetween: (from, to) => monthIndex(to) - monthIndex(from), daysInMonth: ({ year, month }) => monthsOfYear(year)[month - 1]?.[1] ?? null, toDate(year, month, day) { const months = monthsOfYear(year); const length = months[month - 1]?.[1] ?? 0; if (day < 1 || day > length) throw new RangeError(`No Hebrew date ${year}-${month}-${day}`); const daysBefore = months.slice(0, month - 1).reduce((total, [, days]) => total + days, 0); return dateFromFixed(hebrewNewYear(year) + daysBefore + day - 1); } }; export { hebrewArithmeticModel, hebrewFieldsFromFixed, hebrewNewYear, isHebrewLeapYear };