All files / julian index.ts

100% Statements 17/17
100% Branches 7/7
100% Functions 2/2
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                    1x 16x 48x   16x 16x 16x 16x 16x 16x 14x 14x 14x 14x 14x   14x           14x   2x      
 
/**
 * Calculates the Julian Day (JD) for a given date.
 * ---
 * 
 * @param year - The year of the date.
 * @param month - The month of the date.
 * @param day - The day of the date.
 * @returns The calculated Julian Day for the given date.
 */
export default function toJD(year: number, month: number, day: number): number {
  const isInt = (n: number)=>{
    return n % 1 === 0;
  }
  const a = isInt(year);
  const b = isInt(month);
  const c = isInt(day);
  const d = Math.abs(month) < 13;
  const e = Math.abs(day) < 32
  if ( a && b && c && d && e) {
    const a = Math.floor((month - 3) / 12);
    const x4 = year + a;
    const x3 = Math.floor(x4 / 100);
    const x2 = x4 % 100;
    const x1 = month - 12 * a - 3;
    const jd =
      Math.floor((146097 * x3) / 4) +
      Math.floor((36525 * x2) / 100) +
      Math.floor((153 * x1 + 2) / 5) +
      day +
      1721119 -
      0.5;
    return jd ;
  } else {
    return NaN;
  }
}