From 5e5f44c76df3beff68076cb7f7d77e32cb02776e Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 12 Feb 2022 12:06:04 -0500 Subject: [PATCH] Create French to JDN conversion function --- src/Calendar.test.ts | 15 ++++++++++++++- src/Calendar.ts | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Calendar.test.ts b/src/Calendar.test.ts index f2b0e9d..33c5076 100644 --- a/src/Calendar.test.ts +++ b/src/Calendar.test.ts @@ -1,4 +1,4 @@ -import {gregorianJDN} from "./Calendar"; +import {frJDN, gregorianJDN} from './Calendar'; describe('gregorianJDN', () => { it('works', () => { @@ -16,3 +16,16 @@ describe('gregorianJDN', () => { expect(gregorianJDN(7504, 7, 22)).toBe(4462042); }); }); + +describe('frJDN', () => { + it('works for sample dates', () => { + expect(frJDN(1, 1, 1)).toBe(2375840); + expect(frJDN(8, 2, 18)).toBe(2378444); + }); + + it('works in years starting/ending near midnight', () => { + expect(frJDN( 111, 1, 1)).toBe(2416017); // equinox 1902-09-23T23:55:19 UT1 + expect(frJDN( 206, 1, 1)).toBe(2450715); // equinox 1997-09-22T23:55:46 UT1 + expect(frJDN(2490, 1, 1)).toBe(3284926); // equinox 4281-09-20T23:50:38 UT1 + }); +}); diff --git a/src/Calendar.ts b/src/Calendar.ts index 6e45c2a..5fcbaf8 100644 --- a/src/Calendar.ts +++ b/src/Calendar.ts @@ -1,3 +1,15 @@ +import data from '../cal.json'; + +const startJD = data.start_jd; +const startYear = data.start_year; +const leaps: Array = [0]; + +let leapFromStart = 0; +data.leap.forEach(leap => { + leapFromStart += leap; + leaps.push(leapFromStart); +}); + export function gregorianJDN(year: number, month: number, day: number) { const g = year + 4716 - (month <= 2 ? 1 : 0); const f = (month + 9) % 12; @@ -6,3 +18,9 @@ export function gregorianJDN(year: number, month: number, day: number) { const dg = 38 - Math.floor(Math.floor((g + 184) / 100) * 3 / 4); return J + dg; } + +export function frJDN(year: number, month: number, day: number) { + const dy = year - startYear; + const dd = month * 30 + day - 31; + return startJD + 365 * dy + leaps[dy] + dd; +}