From 83e7abec1b2f7b12137138a68b0397de1eb8e2c2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 11 Apr 2022 10:27:28 +0200 Subject: [PATCH] MOBILE-3833 time: Stop using moment in formatTime --- src/core/services/utils/time.ts | 2 +- src/core/singletons/time.ts | 56 ++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/core/services/utils/time.ts b/src/core/services/utils/time.ts index a771a6a21..446181a79 100644 --- a/src/core/services/utils/time.ts +++ b/src/core/services/utils/time.ts @@ -76,7 +76,7 @@ export class CoreTimeUtilsProvider { moment.relativeTimeThreshold('s', 60); moment.relativeTimeThreshold('m', 60); moment.relativeTimeThreshold('h', 24); - moment.relativeTimeThreshold('d', 31); + moment.relativeTimeThreshold('d', 30); moment.relativeTimeThreshold('M', 12); moment.relativeTimeThreshold('y', 365); moment.relativeTimeThreshold('ss', 0); // To display exact number of seconds instead of just "a few seconds". diff --git a/src/core/singletons/time.ts b/src/core/singletons/time.ts index e3a0f0435..6b222937b 100644 --- a/src/core/singletons/time.ts +++ b/src/core/singletons/time.ts @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import moment from 'moment'; +import { Translate } from '@singletons'; +import { CoreConstants } from '../constants'; /** * Singleton with helper functions for time operations. @@ -27,37 +28,54 @@ export class CoreTime { * @return Seconds in a human readable format. */ static formatTime(seconds: number, precision = 2): string { - precision = precision || 6; // Use max precision if 0 is passed. + precision = precision || 5; // Use max precision if 0 is passed. - const eventDuration = moment.duration(Math.abs(seconds), 'seconds'); - let durationString = ''; + const totalSecs = Math.abs(seconds); + if (!totalSecs) { + return Translate.instant('core.now'); + } - if (precision && eventDuration.years() > 0) { - durationString += ' ' + moment.duration(eventDuration.years(), 'years').humanize(); + const years = Math.floor(totalSecs / CoreConstants.SECONDS_YEAR); + let remainder = totalSecs - (years * CoreConstants.SECONDS_YEAR); + const days = Math.floor(remainder / CoreConstants.SECONDS_DAY); + + remainder = totalSecs - (days * CoreConstants.SECONDS_DAY); + + const hours = Math.floor(remainder / CoreConstants.SECONDS_HOUR); + remainder = remainder - (hours * CoreConstants.SECONDS_HOUR); + + const mins = Math.floor(remainder / CoreConstants.SECONDS_MINUTE); + const secs = remainder - (mins * CoreConstants.SECONDS_MINUTE); + + const secondsUnit = Translate.instant('core.' + (secs === 1 ? 'sec' : 'secs')); + const minutesUnit = Translate.instant('core.' + (mins === 1 ? 'min' : 'mins')); + const hoursUnit = Translate.instant('core.' + (hours === 1 ? 'hour' : 'hours')); + const daysUnit = Translate.instant('core.' + (days === 1 ? 'day' : 'days')); + const yearsUnit = Translate.instant('core.' + (years === 1 ? 'year' : 'years')); + const parts: string[] = []; + + if (precision && years) { + parts.push(`${years} ${yearsUnit}`); precision--; } - if (precision && eventDuration.months() > 0) { - durationString += ' ' + moment.duration(eventDuration.months(), 'months').humanize(); + if (precision && days) { + parts.push(`${days} ${daysUnit}`); precision--; } - if (precision && eventDuration.days() > 0) { - durationString += ' ' + moment.duration(eventDuration.days(), 'days').humanize(); + if (precision && hours) { + parts.push(`${hours} ${hoursUnit}`); precision--; } - if (precision && eventDuration.hours() > 0) { - durationString += ' ' + moment.duration(eventDuration.hours(), 'hours').humanize(); + if (precision && mins) { + parts.push(`${mins} ${minutesUnit}`); precision--; } - if (precision && eventDuration.minutes() > 0) { - durationString += ' ' + moment.duration(eventDuration.minutes(), 'minutes').humanize(); - precision--; - } - if (precision && (eventDuration.seconds() > 0 || !durationString)) { - durationString += ' ' + moment.duration(eventDuration.seconds(), 'seconds').humanize(); + if (precision && secs) { + parts.push(`${secs} ${secondsUnit}`); precision--; } - return durationString.trim(); + return parts.join(' '); } /**