smarter time duration unit selection
This commit is contained in:
parent
17dd02ed4e
commit
c601e5dcb4
@ -11,6 +11,15 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy {
|
|||||||
interval: number;
|
interval: number;
|
||||||
text: string;
|
text: string;
|
||||||
units: string[] = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'];
|
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 = {};
|
intervals = {};
|
||||||
|
|
||||||
@Input() time: number;
|
@Input() time: number;
|
||||||
@ -85,8 +94,12 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy {
|
|||||||
|
|
||||||
let counter: number;
|
let counter: number;
|
||||||
for (const [index, unit] of this.units.entries()) {
|
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]);
|
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) {
|
if (counter > 0) {
|
||||||
let rounded = Math.round(seconds / this.intervals[precisionUnit]);
|
let rounded = Math.round(seconds / this.intervals[precisionUnit]);
|
||||||
if (this.fractionDigits) {
|
if (this.fractionDigits) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user