@ishubhamx/panchangam-js
Version:
Enhanced Indian Panchangam (Hindu Calendar) library with comprehensive Vedic features including Muhurta calculations, planetary positions, Rashi placements, and auspicious/inauspicious time calculations
126 lines • 4.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTithiAtTime = getTithiAtTime;
exports.getTithiAtSunrise = getTithiAtSunrise;
exports.doesTithiTouchSunrise = doesTithiTouchSunrise;
exports.getUdayaTithiInfo = getUdayaTithiInfo;
const astronomy_engine_1 = require("astronomy-engine");
const calculations_1 = require("./calculations");
/**
* Get the 1-indexed Tithi (1-30) at an arbitrary point in time.
*
* This is ayanamsa-independent because the Moon−Sun difference cancels any
* constant offset, so tropical longitudes are used directly.
*
* @param time - The instant at which to evaluate
* @returns Tithi index (1-30)
*/
function getTithiAtTime(time) {
const sunLon = (0, astronomy_engine_1.Ecliptic)((0, astronomy_engine_1.GeoVector)(astronomy_engine_1.Body.Sun, time, true)).elon;
const moonLon = (0, astronomy_engine_1.Ecliptic)((0, astronomy_engine_1.GeoVector)(astronomy_engine_1.Body.Moon, time, true)).elon;
return (0, calculations_1.getTithi)(sunLon, moonLon) + 1;
}
/**
* Udaya Tithi Calculation
*
* Get the Tithi prevailing at sunrise (Udaya Tithi).
* This is the correct method for determining festival dates per Hindu tradition.
*
* @param date - Date for which to find Udaya Tithi
* @param sunrise - Sunrise time on that date
* @param observer - Observer location
* @returns Tithi index (1-30) prevailing at sunrise
*/
function getTithiAtSunrise(date, sunrise, observer) {
return getTithiAtTime(sunrise);
}
/**
* Check if a specific Tithi touches (is prevailing at) sunrise
*
* @param tithiIndex - Tithi to check (1-30)
* @param date - Date to check
* @param sunrise - Sunrise time
* @param observer - Observer location
* @returns true if the Tithi is prevailing at sunrise
*/
function doesTithiTouchSunrise(tithiIndex, date, sunrise, observer) {
const udayaTithi = getTithiAtSunrise(date, sunrise, observer);
return udayaTithi === tithiIndex;
}
/**
* Get detailed Udaya Tithi information including start/end times
*
* @param date - Date for calculation
* @param sunrise - Sunrise time
* @param observer - Observer location
* @returns Detailed UdayaTithiInfo
*/
function getUdayaTithiInfo(date, sunrise, observer) {
const udayaTithi = getTithiAtSunrise(date, sunrise, observer);
const paksha = (0, calculations_1.getPaksha)(udayaTithi - 1);
// Find when this Tithi starts and ends
const searchStart = new Date(sunrise.getTime() - 2 * 24 * 60 * 60 * 1000);
const tithiTransition = findTithiTransition(udayaTithi, searchStart, new Date(sunrise.getTime() + 24 * 60 * 60 * 1000), observer);
return {
tithi: udayaTithi,
paksha,
tithiStart: tithiTransition.start,
tithiEnd: tithiTransition.end
};
}
/**
* Helper to find when a specific Tithi starts and ends
*/
function findTithiTransition(targetTithi, searchStart, searchEnd, observer) {
const tithiFunc = (d) => {
const sv = (0, astronomy_engine_1.GeoVector)(astronomy_engine_1.Body.Sun, d, true);
const mv = (0, astronomy_engine_1.GeoVector)(astronomy_engine_1.Body.Moon, d, true);
const sLon = (0, astronomy_engine_1.Ecliptic)(sv).elon;
const mLon = (0, astronomy_engine_1.Ecliptic)(mv).elon;
return (0, calculations_1.getTithi)(sLon, mLon) + 1; // 1-indexed (1-30)
};
// Helper for cyclical distances. -ve means `t` is before `target`. +ve means after.
const getDiff = (t, target) => {
let d = t - target;
if (d < -15)
d += 30;
if (d > 15)
d -= 30;
return d;
};
// Find start time
let lo = searchStart.getTime();
let hi = searchEnd.getTime();
for (let i = 0; i < 50; i++) {
const mid = (lo + hi) / 2;
const tithiAtMid = tithiFunc(new Date(mid));
if (getDiff(tithiAtMid, targetTithi) < 0) {
lo = mid;
}
else {
hi = mid;
}
if (hi - lo < 60000)
break;
}
const startTime = new Date((lo + hi) / 2);
// Find end time
lo = startTime.getTime();
hi = searchEnd.getTime();
for (let i = 0; i < 50; i++) {
const mid = (lo + hi) / 2;
const tithiAtMid = tithiFunc(new Date(mid));
// When we move past the target Tithi, the diff becomes > 0
if (getDiff(tithiAtMid, targetTithi) <= 0) {
lo = mid;
}
else {
hi = mid;
}
if (hi - lo < 60000)
break;
}
const endTime = new Date((lo + hi) / 2);
return { start: startTime, end: endTime };
}
//# sourceMappingURL=udaya-tithi.js.map