mirror of
https://github.com/quantum5/qcal.git
synced 2025-04-25 02:01:56 -04:00
Add JDN to Gregorian date converter
This commit is contained in:
parent
087f4015f7
commit
0c039a04c4
|
@ -1,4 +1,4 @@
|
||||||
import {dateName, frJDN, gregorianJDN, monthName} from './dates';
|
import {dateName, frJDN, gregorianJDN, jdnGregorian, monthName} from './dates';
|
||||||
|
|
||||||
describe('gregorianJDN', () => {
|
describe('gregorianJDN', () => {
|
||||||
it('works', () => {
|
it('works', () => {
|
||||||
|
@ -30,6 +30,27 @@ describe('frJDN', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('jdnGregorian', () => {
|
||||||
|
it('works', () => {
|
||||||
|
expect(jdnGregorian(0)).toEqual(new Date(-4713, 11, 24));
|
||||||
|
expect(jdnGregorian(2299160)).toEqual(new Date(1582, 10, 14));
|
||||||
|
expect(jdnGregorian(2299161)).toEqual(new Date(1582, 10, 15));
|
||||||
|
expect(jdnGregorian(2361221)).toEqual(new Date(1752, 9, 13));
|
||||||
|
expect(jdnGregorian(2361222)).toEqual(new Date(1752, 9, 14));
|
||||||
|
expect(jdnGregorian(2451545)).toEqual(new Date(2000, 1, 1));
|
||||||
|
expect(jdnGregorian(-8512316)).toEqual(new Date(-28019, 12, 20));
|
||||||
|
expect(jdnGregorian(-8534852)).toEqual(new Date(-28080, 4, 8));
|
||||||
|
expect(jdnGregorian(2653462)).toEqual(new Date(2552, 10, 30));
|
||||||
|
expect(jdnGregorian(3271156)).toEqual(new Date(4244, 1, 8));
|
||||||
|
expect(jdnGregorian(-666477)).toEqual(new Date(-6537, 2, 23));
|
||||||
|
expect(jdnGregorian(2397854)).toEqual(new Date(1852, 12, 31));
|
||||||
|
expect(jdnGregorian(-1211235)).toEqual(new Date(-8029, 8, 26));
|
||||||
|
expect(jdnGregorian(-91680)).toEqual(new Date(-4964, 11, 20));
|
||||||
|
expect(jdnGregorian(-5605876)).toEqual(new Date(-20061, 7, 14));
|
||||||
|
expect(jdnGregorian(-295121)).toEqual(new Date(-5521, 11, 19));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('monthName', () => {
|
describe('monthName', () => {
|
||||||
it('works', () => {
|
it('works', () => {
|
||||||
expect(monthName(1)).toBe('Vendémiaire');
|
expect(monthName(1)).toBe('Vendémiaire');
|
||||||
|
|
15
src/dates.ts
15
src/dates.ts
|
@ -34,6 +34,12 @@ export type Day =
|
||||||
| 29
|
| 29
|
||||||
| 30;
|
| 30;
|
||||||
|
|
||||||
|
export type GregorianDate = {
|
||||||
|
year: number,
|
||||||
|
month: number,
|
||||||
|
day: number,
|
||||||
|
};
|
||||||
|
|
||||||
const monthNames: {
|
const monthNames: {
|
||||||
[key in Month]: string
|
[key in Month]: string
|
||||||
} = {
|
} = {
|
||||||
|
@ -81,6 +87,15 @@ export function frJDN(year: number, month: Month, day: Day): number {
|
||||||
return startJD + 365 * dy + leaps[dy] + dd;
|
return startJD + 365 * dy + leaps[dy] + dd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function jdnGregorian(jdn: number): Date {
|
||||||
|
const e = 4 * (jdn + 1401 + Math.floor(Math.floor((4 * jdn + 274277) / 146097) * 3 / 4) - 38) + 3;
|
||||||
|
const h = 5 * Math.floor((e % 1461 + 1461) % 1461 / 4) + 2;
|
||||||
|
const day = Math.floor((h % 153 + 153) % 153 / 5) + 1;
|
||||||
|
const month = (Math.floor(h / 153) + 2) % 12 + 1;
|
||||||
|
const year = Math.floor(e / 1461) - 4716 + Math.floor((14 - month) / 12);
|
||||||
|
return new Date(year, month, day);
|
||||||
|
}
|
||||||
|
|
||||||
export function monthName(month: Month): string {
|
export function monthName(month: Month): string {
|
||||||
return monthNames[month];
|
return monthNames[month];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue