mirror of
https://github.com/quantum5/qcal.git
synced 2025-04-25 02:01:56 -04:00
Add JDN to French Republican calculation
This commit is contained in:
parent
d366604665
commit
2c3657248b
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
import {Calendar} from './Calendar';
|
import {Calendar} from './Calendar';
|
||||||
import {gregorianJDN, Month} from "./dates";
|
import {gregorianJDN, jdnFrench, Month} from "./dates";
|
||||||
|
|
||||||
type YearMonth = {
|
type YearMonth = {
|
||||||
year: number;
|
year: number;
|
||||||
|
@ -28,12 +28,13 @@ class App extends React.Component<{}, AppState> {
|
||||||
|
|
||||||
constructor(props: {}) {
|
constructor(props: {}) {
|
||||||
super(props);
|
super(props);
|
||||||
const current = {year: 230, month: 5};
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
|
const todayJDN = gregorianJDN(today.getFullYear(), today.getMonth() + 1, today.getDay());
|
||||||
|
const {year, month} = jdnFrench(todayJDN);
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
...(parseURL() || current as YearMonth),
|
...(parseURL() || {year, month}),
|
||||||
todayJDN: gregorianJDN(today.getFullYear(), today.getMonth() + 1, today.getDay()),
|
todayJDN,
|
||||||
};
|
};
|
||||||
this.updateURL();
|
this.updateURL();
|
||||||
this.updateStateFromURL = this.updateStateFromURL.bind(this);
|
this.updateStateFromURL = this.updateStateFromURL.bind(this);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {dateName, frJDN, gregorianJDN, jdnGregorian, jdnLongCount, monthName} from './dates';
|
import {dateName, frJDN, gregorianJDN, jdnFrench, jdnGregorian, jdnLongCount, monthName} from './dates';
|
||||||
|
|
||||||
describe('gregorianJDN', () => {
|
describe('gregorianJDN', () => {
|
||||||
it('works', () => {
|
it('works', () => {
|
||||||
|
@ -30,6 +30,22 @@ describe('frJDN', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('jdnFrench', () => {
|
||||||
|
it('works for sample dates', () => {
|
||||||
|
expect(jdnFrench(2375840)).toEqual({year: 1, month: 1, day: 1});
|
||||||
|
expect(jdnFrench(2378444)).toEqual({year: 8, month: 2, day: 18});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works in years starting/ending near midnight', () => {
|
||||||
|
expect(jdnFrench(2416017)).toEqual({year: 111, month: 1, day: 1});
|
||||||
|
expect(jdnFrench(2450715)).toEqual({year: 206, month: 1, day: 1});
|
||||||
|
expect(jdnFrench(3284926)).toEqual({year: 2490, month: 1, day: 1});
|
||||||
|
expect(jdnFrench(2416016)).toEqual({year: 110, month: 13, day: 6});
|
||||||
|
expect(jdnFrench(2450714)).toEqual({year: 205, month: 13, day: 6});
|
||||||
|
expect(jdnFrench(3284925)).toEqual({year: 2489, month: 13, day: 5});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('jdnGregorian', () => {
|
describe('jdnGregorian', () => {
|
||||||
it('works', () => {
|
it('works', () => {
|
||||||
expect(jdnGregorian(0)).toEqual(new Date(-4713, 10, 24));
|
expect(jdnGregorian(0)).toEqual(new Date(-4713, 10, 24));
|
||||||
|
|
26
src/dates.ts
26
src/dates.ts
|
@ -34,10 +34,10 @@ export type Day =
|
||||||
| 29
|
| 29
|
||||||
| 30;
|
| 30;
|
||||||
|
|
||||||
export type GregorianDate = {
|
export type FrenchDate = {
|
||||||
year: number,
|
year: number,
|
||||||
month: number,
|
month: Month,
|
||||||
day: number,
|
day: Day,
|
||||||
};
|
};
|
||||||
|
|
||||||
const monthNames: {
|
const monthNames: {
|
||||||
|
@ -96,6 +96,26 @@ export function jdnGregorian(jdn: number): Date {
|
||||||
return new Date(year, month - 1, day);
|
return new Date(year, month - 1, day);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function jdnFrench(jdn: number): FrenchDate {
|
||||||
|
let lo = 0;
|
||||||
|
let hi = leaps.length;
|
||||||
|
|
||||||
|
while (lo + 1 < hi) {
|
||||||
|
const mid = Math.floor((lo + hi) / 2);
|
||||||
|
if (startJD + 365 * mid + leaps[mid] <= jdn)
|
||||||
|
lo = mid;
|
||||||
|
else
|
||||||
|
hi = mid;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dd = jdn - (startJD + 365 * lo + leaps[lo]);
|
||||||
|
return {
|
||||||
|
year: startYear + lo,
|
||||||
|
month: Math.floor(dd / 30) + 1 as Month,
|
||||||
|
day: dd % 30 + 1 as Day,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function jdnLongCount(jdn: number): string | null {
|
export function jdnLongCount(jdn: number): string | null {
|
||||||
let z = jdn - 584283;
|
let z = jdn - 584283;
|
||||||
if (z < 0)
|
if (z < 0)
|
||||||
|
|
Loading…
Reference in a new issue