diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-10-14 17:52:29 +1100 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-10-14 17:52:29 +1100 |
| commit | 4a0ffc55734adaaf3e9eacd9ee4bbc212e96a71e (patch) | |
| tree | b2d2df0175097c0e73c56d35e484a9685df7286e /src/lib.ts | |
| parent | 48d5f04d82cfd2e49644c2d5a55256b0088520b5 (diff) | |
Account for there being no year 0 CE
Diffstat (limited to 'src/lib.ts')
| -rw-r--r-- | src/lib.ts | 29 |
1 files changed, 27 insertions, 2 deletions
@@ -115,13 +115,20 @@ export class HistDate { later = this as HistDate; } // - const yearDiff = later.year - earlier.year; + const yearDiff = earlier.getYearDiff(later); if (yearDiff == 0){ return later.month - earlier.month; } else { return (13 - earlier.month) + (yearDiff - 1) * 12 + later.month - 1; } } + getYearDiff(other: HistDate){ + let yearDiff = Math.abs(this.year - other.year); + if (this.year * other.year < 0){ // Account for no 0 CE + yearDiff -= 1; + } + return yearDiff; + } clone(){ return new HistDate(this.year, this.month, this.day); } @@ -181,6 +188,9 @@ export function stepDate(date: HistDate, scale: number, {forward=true, count=1, newDate.month += 1; } else { newDate.year += 1; + if (newDate.year == 0){ + newDate.year = 1; + } newDate.month = 1; } } else { @@ -188,11 +198,26 @@ export function stepDate(date: HistDate, scale: number, {forward=true, count=1, newDate.month -= 1; } else { newDate.year -= 1; + if (newDate.year == 0){ + newDate.year = -1; + } newDate.month = 12; } } } else { - newDate.year += forward ? scale : -scale; + let newYear; + if (forward){ + newYear = newDate.year + scale; + if (newDate.year < 0 && newYear >= 0){ // If different sign, account for there being no 0 CE + newYear += 1; + } + } else { + newYear = newDate.year - scale; + if (newDate.year > 0 && newYear <= 0){ + newYear -= 1; + } + } + newDate.year = newYear; } } return newDate; |
