MOBILE-3833 time: Stop using moment in formatTime

main
Dani Palou 2022-04-11 10:27:28 +02:00
parent 4747c3bf20
commit 83e7abec1b
2 changed files with 38 additions and 20 deletions

View File

@ -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".

View File

@ -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(' ');
}
/**