Create Gregorian to JDN conversion function

This commit is contained in:
Quantum 2022-02-12 11:48:35 -05:00
parent 6393754f8f
commit 096858975b
2 changed files with 26 additions and 0 deletions

18
src/Calendar.test.ts Normal file
View file

@ -0,0 +1,18 @@
import {gregorianJDN} from "./Calendar";
describe('gregorianJDN', () => {
it('works', () => {
expect(gregorianJDN(2000, 1, 1)).toBe(2451545);
expect(gregorianJDN(-4713, 11, 24)).toBe(0);
expect(gregorianJDN(11917, 9, 18)).toBe(6073915);
expect(gregorianJDN(-28565, 6, 17)).toBe(-8711925);
expect(gregorianJDN(-26650, 4, 13)).toBe(-8012550);
expect(gregorianJDN(17430, 3, 8)).toBe(8087303);
expect(gregorianJDN(3395, 7, 18)).toBe(2961257);
expect(gregorianJDN(4579, 3, 11)).toBe(3393575);
expect(gregorianJDN(-14851, 11, 22)).toBe(-3702831);
expect(gregorianJDN(8824, 11, 28)).toBe(4944292);
expect(gregorianJDN(19720, 8, 14)).toBe(8923868);
expect(gregorianJDN(7504, 7, 22)).toBe(4462042);
});
});

8
src/Calendar.ts Normal file
View file

@ -0,0 +1,8 @@
export function gregorianJDN(year: number, month: number, day: number) {
const g = year + 4716 - (month <= 2 ? 1 : 0);
const f = (month + 9) % 12;
const e = Math.floor(1461 * g / 4) + day - 1402;
const J = e + Math.floor((153 * f + 2) / 5);
const dg = 38 - Math.floor(Math.floor((g + 184) / 100) * 3 / 4);
return J + dg;
}