| | import { daysInHebrewMonth, monthsInHebrewYear } from "./calendarMath.js"; |
| | import { type HebrewDate, MONTHS_PER_CYCLE } from "./constants.js"; |
| |
|
| | |
| | |
| | |
| | |
| | function monthsBeforeYear(year: number): number { |
| | if (year <= 1) { |
| | return 0; |
| | } |
| | const cycles = Math.floor((year - 1) / 19); |
| | let months = cycles * MONTHS_PER_CYCLE; |
| | let currentYear = cycles * 19 + 1; |
| | while (currentYear < year) { |
| | months += monthsInHebrewYear(currentYear); |
| | currentYear += 1; |
| | } |
| | return months; |
| | } |
| |
|
| | |
| | export function monthsSinceEpoch({ |
| | year, |
| | monthIndex, |
| | }: Pick<HebrewDate, "year" | "monthIndex">): number { |
| | return monthsBeforeYear(year) + monthIndex; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | function hebrewFromMonthIndex(monthIndex: number): { |
| | year: number; |
| | month: number; |
| | } { |
| | let index = monthIndex; |
| | let year = 1; |
| | if (index >= 0) { |
| | const cycles = Math.floor(index / MONTHS_PER_CYCLE); |
| | year += cycles * 19; |
| | index -= cycles * MONTHS_PER_CYCLE; |
| | while (true) { |
| | const months = monthsInHebrewYear(year); |
| | if (index < months) { |
| | break; |
| | } |
| | index -= months; |
| | year += 1; |
| | } |
| | return { year, month: index }; |
| | } |
| | |
| | while (index < 0) { |
| | year -= 1; |
| | const months = monthsInHebrewYear(year); |
| | index += months; |
| | } |
| | return { year, month: index }; |
| | } |
| |
|
| | |
| | export function clampHebrewDay( |
| | year: number, |
| | monthIndex: number, |
| | day: number, |
| | ): number { |
| | const maxDay = daysInHebrewMonth(year, monthIndex); |
| | return Math.min(day, maxDay); |
| | } |
| |
|
| | |
| | export function monthIndexToHebrewDate( |
| | monthIndex: number, |
| | day: number, |
| | ): HebrewDate { |
| | const { year, month } = hebrewFromMonthIndex(monthIndex); |
| | return { |
| | year, |
| | monthIndex: month, |
| | day: clampHebrewDay(year, month, day), |
| | }; |
| | } |
| |
|
| | |
| | export function hebrewMonthNumber(monthIndex: number): number { |
| | return monthIndex + 1; |
| | } |
| |
|