smarter time duration unit selection

This commit is contained in:
Mononaut 2023-05-11 13:33:38 -06:00
parent 17dd02ed4e
commit c601e5dcb4
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -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) {