diff options
| -rw-r--r-- | src/components/TimeLine.vue | 4 | ||||
| -rw-r--r-- | src/lib.ts | 29 |
2 files changed, 29 insertions, 4 deletions
diff --git a/src/components/TimeLine.vue b/src/components/TimeLine.vue index 01c937c..8c0bbb3 100644 --- a/src/components/TimeLine.vue +++ b/src/components/TimeLine.vue @@ -123,7 +123,7 @@ function initScale(){ } function getYearlyScale(startDate: HistDate, endDate: HistDate, availLen: number){ // Get the smallest yearly scale that divides a date range, without making ticks too close - let yearDiff = endDate.year - startDate.year; + let yearDiff = startDate.getYearDiff(endDate); let idx = 0; while (SCALES[idx] >= yearDiff){ // Get scale with units smaller than yearDiff idx += 1; @@ -160,7 +160,7 @@ function getNumVisibleUnits(): number { } else if (scale.value == MONTH_SCALE){ numUnits = startDate.value.getMonthDiff(endDate.value); } else { - numUnits = (endDate.value.year - startDate.value.year) / scale.value; + numUnits = startDate.value.getYearDiff(endDate.value) / scale.value; } return numUnits + startOffset.value + endOffset.value; } @@ -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; |
