From c601e5dcb4c2c1fe7360648f7f27f5bd4e644d26 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 11 May 2023 13:33:38 -0600 Subject: [PATCH] smarter time duration unit selection --- .../src/app/components/time/time.component.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/frontend/src/app/components/time/time.component.ts b/frontend/src/app/components/time/time.component.ts index 6431f0b39..f2b220d39 100644 --- a/frontend/src/app/components/time/time.component.ts +++ b/frontend/src/app/components/time/time.component.ts @@ -11,6 +11,15 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { interval: number; text: string; units: string[] = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']; + precisionThresholds = { + year: 100, + month: 18, + week: 12, + day: 31, + hour: 48, + minute: 90, + second: 90 + }; intervals = {}; @Input() time: number; @@ -85,8 +94,12 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { let counter: number; for (const [index, unit] of this.units.entries()) { - const precisionUnit = this.units[Math.min(this.units.length - 1), index + this.precision]; + let precisionUnit = this.units[Math.min(this.units.length - 1, index + this.precision)]; counter = Math.floor(seconds / this.intervals[unit]); + const precisionCounter = Math.floor(seconds / this.intervals[precisionUnit]); + if (precisionCounter > this.precisionThresholds[precisionUnit]) { + precisionUnit = unit; + } if (counter > 0) { let rounded = Math.round(seconds / this.intervals[precisionUnit]); if (this.fractionDigits) {