Add month and date functions

This commit is contained in:
Quantum 2022-02-12 12:27:14 -05:00
parent 5e5f44c76d
commit 402ddb81ed
3 changed files with 122 additions and 27 deletions

View file

@ -1,26 +0,0 @@
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;
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;
}
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;
}

View file

@ -1,4 +1,4 @@
import {frJDN, gregorianJDN} from './Calendar'; import {dateName, frJDN, gregorianJDN, monthName} from './dates';
describe('gregorianJDN', () => { describe('gregorianJDN', () => {
it('works', () => { it('works', () => {
@ -29,3 +29,27 @@ describe('frJDN', () => {
expect(frJDN(2490, 1, 1)).toBe(3284926); // equinox 4281-09-20T23:50:38 UT1 expect(frJDN(2490, 1, 1)).toBe(3284926); // equinox 4281-09-20T23:50:38 UT1
}); });
}); });
describe('monthName', () => {
it('works', () => {
expect(monthName(1)).toBe('Vendémiaire');
expect(monthName(12)).toBe('Fructidor');
expect(monthName(13)).toBe('Jours Complémentaires');
});
});
describe('dateName', () => {
it('works', () => {
expect(dateName(1, 1)).toBe('1 Vendémiaire');
expect(dateName(2, 18)).toBe('18 Brumaire');
expect(dateName(3, 11)).toBe('11 Frimaire');
expect(dateName(8, 16)).toBe('16 Floréal');
expect(dateName(12, 30)).toBe('30 Fructidor');
expect(dateName(13, 1)).toBe('La Fête de la Vertu');
expect(dateName(13, 6)).toBe('La Fête de la Révolution');
});
it('returns null for non-existent complimentary days', () => {
expect(dateName(13, 7)).toBeNull();
});
});

97
src/dates.ts Normal file
View file

@ -0,0 +1,97 @@
import data from '../cal.json';
// Month 13 is for the complementary days
export type Month = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13;
export type Day =
1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 17
| 18
| 19
| 20
| 21
| 22
| 23
| 24
| 25
| 26
| 27
| 28
| 29
| 30;
const monthNames: {
[key in Month]: string
} = {
1: 'Vendémiaire',
2: 'Brumaire',
3: 'Frimaire',
4: 'Nivôse',
5: 'Pluviôse',
6: 'Ventôse',
7: 'Germinal',
8: 'Floréal',
9: 'Prairial',
10: 'Messidor',
11: 'Thermidor',
12: 'Fructidor',
13: 'Jours Complémentaires',
};
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): 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;
}
export function frJDN(year: number, month: Month, day: Day): number {
const dy = year - startYear;
const dd = month * 30 + day - 31;
return startJD + 365 * dy + leaps[dy] + dd;
}
export function monthName(month: Month): string {
return monthNames[month];
}
export function dateName(month: Month, day: Day): string | null {
if (month === 13) {
switch (day) {
case 1: return 'La Fête de la Vertu';
case 2: return 'La Fête du Génie';
case 3: return 'La Fête du Travail';
case 4: return "La Fête de l'Opinion";
case 5: return 'La Fête des Récompenses';
case 6: return 'La Fête de la Révolution';
default: return null;
}
}
return `${day} ${monthNames[month]}`;
}