Deal with out-of-bounds years

This commit is contained in:
Quantum 2022-02-12 17:58:54 -05:00
parent a473693f69
commit 54c9137b49
3 changed files with 25 additions and 8 deletions

View file

@ -1,7 +1,7 @@
import React from 'react';
import './App.css';
import {Calendar} from './Calendar';
import {gregorianJDN, jdnFrench, Month} from "./dates";
import {frSupportedYear, gregorianJDN, jdnFrench, Month} from "./dates";
type YearMonth = {
year: number;
@ -14,9 +14,10 @@ function parseURL(): YearMonth | null {
return null;
const month = +match[2];
if (month < 1 || month > 13)
const year = +match[1];
if (!frSupportedYear(year) || month < 1 || month > 13)
return null;
return {year: +match[1], month: month as Month};
return {year: year, month: month as Month};
}
type AppState = YearMonth & {

View file

@ -4,13 +4,15 @@ import {
dateName,
Day,
decadeNames,
endYear,
frIsLeap,
frJDN,
jdnFrench,
jdnGregorian,
jdnLongCount,
Month,
monthName
monthName,
startYear
} from './dates';
type MonthProps = {
@ -116,6 +118,14 @@ export class Calendar extends React.Component<CalendarProps, CalendarState> {
month -= 13;
}
if (year < startYear) {
year = startYear;
month = 1;
} else if (year > endYear) {
year = endYear;
month = 13;
}
this.props.onSwitch && this.props.onSwitch(year, month as Month);
}
@ -150,13 +160,13 @@ export class Calendar extends React.Component<CalendarProps, CalendarState> {
}
monthChange(event: any) {
this.props.onSwitch && this.props.onSwitch(this.props.year, event.target.value as Month);
this.goToNormalized(this.props.year, event.target.value as Month);
}
yearChange(event: any) {
console.log(/^-?\d+/.test(event.target.value));
if (/^-?\d+/.test(event.target.value)) {
this.props.onSwitch && this.props.onSwitch(+event.target.value, this.props.month);
this.goToNormalized(+event.target.value, this.props.month);
}
this.setState({yearStr: event.target.value});
}
@ -202,7 +212,7 @@ export class Calendar extends React.Component<CalendarProps, CalendarState> {
})
}</select>
<input type="number" className="Calendar-year-input form-control" value={this.state.yearStr}
onChange={this.yearChange.bind(this)}/>
onChange={this.yearChange.bind(this)} min={startYear} max={endYear}/>
<button type="button" className="form-control btn btn-primary Calendar-today-button"
onClick={this.goToToday.bind(this)}>Today
</button>

View file

@ -63,15 +63,21 @@ export const decadeNames = [
];
const startJD = data.start_jd;
const startYear = data.start_year;
export const startYear = data.start_year;
const leaps: Array<number> = [0];
let leapFromStart = 0;
data.leap.forEach(leap => {
leapFromStart += leap;
leaps.push(leapFromStart);
});
export const endYear = startYear + leaps.length - 1;
export function frSupportedYear(year: number): boolean {
return startYear <= year && year <= endYear;
}
export function gregorianJDN(year: number, month: number, day: number): number {
const g = year + 4716 - (month <= 2 ? 1 : 0);
const f = (month + 9) % 12;