Create French to JDN conversion function

This commit is contained in:
Quantum 2022-02-12 12:06:04 -05:00
parent 096858975b
commit 5e5f44c76d
2 changed files with 32 additions and 1 deletions

View file

@ -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
});
});

View file

@ -1,3 +1,15 @@
import data from '../cal.json';
const startJD = data.start_jd;
const startYear = data.start_year;
const leaps: Array<number> = [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;
}