mirror of
https://github.com/quantum5/qcal.git
synced 2025-04-24 17:51:57 -04:00
Deal with out-of-bounds years
This commit is contained in:
parent
a473693f69
commit
54c9137b49
|
@ -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, jdnFrench, Month} from "./dates";
|
import {frSupportedYear, gregorianJDN, jdnFrench, Month} from "./dates";
|
||||||
|
|
||||||
type YearMonth = {
|
type YearMonth = {
|
||||||
year: number;
|
year: number;
|
||||||
|
@ -14,9 +14,10 @@ function parseURL(): YearMonth | null {
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
const month = +match[2];
|
const month = +match[2];
|
||||||
if (month < 1 || month > 13)
|
const year = +match[1];
|
||||||
|
if (!frSupportedYear(year) || month < 1 || month > 13)
|
||||||
return null;
|
return null;
|
||||||
return {year: +match[1], month: month as Month};
|
return {year: year, month: month as Month};
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppState = YearMonth & {
|
type AppState = YearMonth & {
|
||||||
|
|
|
@ -4,13 +4,15 @@ import {
|
||||||
dateName,
|
dateName,
|
||||||
Day,
|
Day,
|
||||||
decadeNames,
|
decadeNames,
|
||||||
|
endYear,
|
||||||
frIsLeap,
|
frIsLeap,
|
||||||
frJDN,
|
frJDN,
|
||||||
jdnFrench,
|
jdnFrench,
|
||||||
jdnGregorian,
|
jdnGregorian,
|
||||||
jdnLongCount,
|
jdnLongCount,
|
||||||
Month,
|
Month,
|
||||||
monthName
|
monthName,
|
||||||
|
startYear
|
||||||
} from './dates';
|
} from './dates';
|
||||||
|
|
||||||
type MonthProps = {
|
type MonthProps = {
|
||||||
|
@ -116,6 +118,14 @@ export class Calendar extends React.Component<CalendarProps, CalendarState> {
|
||||||
month -= 13;
|
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);
|
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) {
|
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) {
|
yearChange(event: any) {
|
||||||
console.log(/^-?\d+/.test(event.target.value));
|
console.log(/^-?\d+/.test(event.target.value));
|
||||||
if (/^-?\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});
|
this.setState({yearStr: event.target.value});
|
||||||
}
|
}
|
||||||
|
@ -202,7 +212,7 @@ export class Calendar extends React.Component<CalendarProps, CalendarState> {
|
||||||
})
|
})
|
||||||
}</select>
|
}</select>
|
||||||
<input type="number" className="Calendar-year-input form-control" value={this.state.yearStr}
|
<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"
|
<button type="button" className="form-control btn btn-primary Calendar-today-button"
|
||||||
onClick={this.goToToday.bind(this)}>Today
|
onClick={this.goToToday.bind(this)}>Today
|
||||||
</button>
|
</button>
|
||||||
|
|
|
@ -63,15 +63,21 @@ export const decadeNames = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const startJD = data.start_jd;
|
const startJD = data.start_jd;
|
||||||
const startYear = data.start_year;
|
export const startYear = data.start_year;
|
||||||
const leaps: Array<number> = [0];
|
const leaps: Array<number> = [0];
|
||||||
|
|
||||||
let leapFromStart = 0;
|
let leapFromStart = 0;
|
||||||
data.leap.forEach(leap => {
|
data.leap.forEach(leap => {
|
||||||
leapFromStart += leap;
|
leapFromStart += leap;
|
||||||
leaps.push(leapFromStart);
|
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 {
|
export function gregorianJDN(year: number, month: number, day: number): number {
|
||||||
const g = year + 4716 - (month <= 2 ? 1 : 0);
|
const g = year + 4716 - (month <= 2 ? 1 : 0);
|
||||||
const f = (month + 9) % 12;
|
const f = (month + 9) % 12;
|
||||||
|
|
Loading…
Reference in a new issue