diff options
| author | Terry Truong <terry06890@gmail.com> | 2023-01-04 11:44:51 +1100 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2023-01-04 12:06:34 +1100 |
| commit | ee935784ccdf6e75a853f80b78dac6bf7fb95fbe (patch) | |
| tree | 1da0d852de78ad17a373ca596b3a6c2d5f2d885a | |
| parent | 79a4c7385b0330a6e332726d6ba7bc4ebb27ddf5 (diff) | |
Remove server-side conversion of YearDates >4713 BC to CalDates
The conversion was originally intended to simply the code. But,
as events may span years before and after 4713 BC, the conversion
introduces the possibility of having both YearDates and CalDates
in the same event, which is unhelpful.
| -rw-r--r-- | backend/hist_data/cal.py | 5 | ||||
| -rw-r--r-- | src/components/TimeLine.vue | 5 | ||||
| -rw-r--r-- | src/lib.ts | 49 |
3 files changed, 27 insertions, 32 deletions
diff --git a/backend/hist_data/cal.py b/backend/hist_data/cal.py index 550303e..29959ef 100644 --- a/backend/hist_data/cal.py +++ b/backend/hist_data/cal.py @@ -94,10 +94,7 @@ class HistDate: def dbDateToHistDate(n: int, fmt: int, end=False) -> HistDate: """ Converts a start/start_upper/etc and fmt value in the 'events' db table, into a HistDate """ if fmt == 0: # year - if n >= MIN_CAL_YEAR: - return HistDate(True, n, 1, 1) - else: - return HistDate(None, n) + return HistDate(None, n) elif fmt == 1 or fmt == 3 and end: # jdn for gregorian calendar return HistDate(True, *jdnToGregorian(n)) else: # fmt == 2 or fmt == 3 and not end diff --git a/src/components/TimeLine.vue b/src/components/TimeLine.vue index 5a69f3f..a2c77a3 100644 --- a/src/components/TimeLine.vue +++ b/src/components/TimeLine.vue @@ -666,16 +666,17 @@ const tickToCount = computed((): Map<number, number> => { } const map = props.unitCountMaps[minorScaleIdx.value]; for (let tickIdx = firstIdx.value; tickIdx < lastIdx.value; tickIdx++){ - tickToCount.set(tickIdx, 0); + let eventCount = 0; let date = ticks.value[tickIdx].date.clone(); let nextDate = ticks.value[tickIdx + 1].date; while (date.isEarlier(nextDate)){ let unit = dateToUnit(date, minorScale.value); if (map.has(unit)){ - tickToCount.set(tickIdx, tickToCount.get(tickIdx)! + map.get(unit)!); + eventCount += map.get(unit)!; } stepDate(date, minorScale.value, {inplace: true}); } + tickToCount.set(tickIdx, eventCount); } return tickToCount; }); @@ -92,7 +92,7 @@ export class HistDate { this.month = gcal == null ? 1 : month; this.day = gcal == null ? 1 : day; } - equals(other: HistDate, scale=DAY_SCALE){ + equals(other: HistDate, scale=DAY_SCALE){ // Does not check gcal if (scale == DAY_SCALE){ return this.year == other.year && this.month == other.month && this.day == other.day; } else if (scale == MONTH_SCALE){ @@ -221,10 +221,10 @@ export class YearDate extends HistDate { declare year: number; declare month: 1; declare day: 1; - constructor(year=MIN_CAL_YEAR-1){ - if (year >= MIN_CAL_YEAR){ - throw new Error(`Year must be before ${MIN_CAL_YEAR}`); - } + constructor(year: number){ + // Note: Intentionally not enforcing year < MIN_CAL_YEAR here. This does mean a YearDate can be + // interpreted as the same day as a CalDate, but it also avoids having HistEvents that span across + // MIN_CAL_YEAR that have a mix of YearDates and CalDates. super(null, year, 1, 1); } } @@ -315,25 +315,21 @@ export type EventResponseJson = { events: HistEventJson[], unitCounts: {[x: number]: number} | null, } -export function jsonToHistDate(json: HistDateJson){ - if (json.gcal == null){ - return new YearDate(json.year); - } else { - return new CalDate(json.year, json.month, json.day, json.gcal); - } -} -export function jsonToHistEvent(json: HistEventJson){ - return { - id: json.id, - title: json.title, - start: jsonToHistDate(json.start), - startUpper: json.startUpper == null ? null : jsonToHistDate(json.startUpper), - end: json.end == null ? null : jsonToHistDate(json.end), - endUpper: json.endUpper == null ? null : jsonToHistDate(json.endUpper), - ctg: json.ctg, - imgId: json.imgId, - pop: json.pop, - }; +export function jsonToHistDate(json: HistDateJson): HistDate{ + return new HistDate(json.gcal, json.year, json.month, json.day); +} +export function jsonToHistEvent(json: HistEventJson): HistEvent { + return new HistEvent( + json.id, + json.title, + jsonToHistDate(json.start), + json.startUpper == null ? null : jsonToHistDate(json.startUpper), + json.end == null ? null : jsonToHistDate(json.end), + json.endUpper == null ? null : jsonToHistDate(json.endUpper), + json.ctg, + json.imgId, + json.pop, + ); } // For dates in a timeline @@ -506,6 +502,7 @@ export function getEventPrecision(event: HistEvent): number { return Number.POSITIVE_INFINITY; } export function dateToUnit(date: HistDate, scale: number): number { + // For a YearDate and sub-yearly scale, uses the first day of the YearDate's year if (scale >= 1){ return Math.floor(date.year / scale); } else if (scale == MONTH_SCALE){ @@ -551,7 +548,7 @@ export class DateRangeTree { } add(range: DateRange){ const rangesToRemove: HistDate[] = []; // Holds starts of ranges to remove - const dummyDate = new YearDate(); + const dummyDate = new YearDate(1); // Find ranges to remove const itr = this.tree.lowerBound([range[0], dummyDate]); let prevRange = itr.prev(); @@ -585,7 +582,7 @@ export class DateRangeTree { this.tree.insert([startDate, endDate]); } contains(range: DateRange): boolean { - const itr = this.tree.lowerBound([range[0], new YearDate()]); + const itr = this.tree.lowerBound([range[0], new YearDate(1)]); let r = itr.data(); if (r == null){ r = itr.prev(); |
