2017-11-06 17:17:11 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import * as moment from 'moment';
|
|
|
|
import { CoreConstants } from '../../core/constants';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "Utils" service with helper functions for date and time.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreTimeUtilsProvider {
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
constructor(private translate: TranslateService) { }
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns hours, minutes and seconds in a human readable format
|
|
|
|
*
|
|
|
|
* @param {number} seconds A number of seconds
|
|
|
|
* @return {string} Seconds in a human readable format.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
formatTime(seconds: number): string {
|
|
|
|
let totalSecs,
|
|
|
|
years,
|
|
|
|
days,
|
|
|
|
hours,
|
|
|
|
mins,
|
|
|
|
secs,
|
|
|
|
remainder;
|
|
|
|
|
|
|
|
totalSecs = Math.abs(seconds);
|
|
|
|
years = Math.floor(totalSecs / CoreConstants.SECONDS_YEAR);
|
|
|
|
remainder = totalSecs - (years * CoreConstants.SECONDS_YEAR);
|
|
|
|
days = Math.floor(remainder / CoreConstants.SECONDS_DAY);
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-01-12 14:28:46 +01:00
|
|
|
remainder = totalSecs - (days * CoreConstants.SECONDS_DAY);
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
hours = Math.floor(remainder / CoreConstants.SECONDS_HOUR);
|
2018-01-12 14:28:46 +01:00
|
|
|
remainder = remainder - (hours * CoreConstants.SECONDS_HOUR);
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
mins = Math.floor(remainder / CoreConstants.SECONDS_MINUTE);
|
|
|
|
secs = remainder - (mins * CoreConstants.SECONDS_MINUTE);
|
|
|
|
|
|
|
|
const ss = this.translate.instant('core.' + (secs == 1 ? 'sec' : 'secs')),
|
2017-12-08 15:53:27 +01:00
|
|
|
sm = this.translate.instant('core.' + (mins == 1 ? 'min' : 'mins')),
|
|
|
|
sh = this.translate.instant('core.' + (hours == 1 ? 'hour' : 'hours')),
|
|
|
|
sd = this.translate.instant('core.' + (days == 1 ? 'day' : 'days')),
|
2018-01-29 10:05:20 +01:00
|
|
|
sy = this.translate.instant('core.' + (years == 1 ? 'year' : 'years'));
|
|
|
|
let oyears = '',
|
2017-11-06 17:17:11 +01:00
|
|
|
odays = '',
|
|
|
|
ohours = '',
|
|
|
|
omins = '',
|
|
|
|
osecs = '';
|
|
|
|
|
|
|
|
if (years) {
|
2018-01-29 10:05:20 +01:00
|
|
|
oyears = years + ' ' + sy;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
if (days) {
|
2018-01-29 10:05:20 +01:00
|
|
|
odays = days + ' ' + sd;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
if (hours) {
|
|
|
|
ohours = hours + ' ' + sh;
|
|
|
|
}
|
|
|
|
if (mins) {
|
2018-01-29 10:05:20 +01:00
|
|
|
omins = mins + ' ' + sm;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
if (secs) {
|
2018-01-29 10:05:20 +01:00
|
|
|
osecs = secs + ' ' + ss;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (years) {
|
|
|
|
return oyears + ' ' + odays;
|
|
|
|
}
|
|
|
|
if (days) {
|
|
|
|
return odays + ' ' + ohours;
|
|
|
|
}
|
|
|
|
if (hours) {
|
|
|
|
return ohours + ' ' + omins;
|
|
|
|
}
|
|
|
|
if (mins) {
|
|
|
|
return omins + ' ' + osecs;
|
|
|
|
}
|
|
|
|
if (secs) {
|
|
|
|
return osecs;
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:53:27 +01:00
|
|
|
return this.translate.instant('core.now');
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns hours, minutes and seconds in a human readable format.
|
|
|
|
*
|
|
|
|
* @param {number} duration Duration in seconds
|
|
|
|
* @param {number} [precision] Number of elements to have in precission. 0 or undefined to full precission.
|
|
|
|
* @return {string} Duration in a human readable format.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
formatDuration(duration: number, precision?: number): string {
|
2017-11-06 17:17:11 +01:00
|
|
|
precision = precision || 5;
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
const eventDuration = moment.duration(duration, 'seconds');
|
|
|
|
let durationString = '';
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
if (precision && eventDuration.years() > 0) {
|
|
|
|
durationString += ' ' + moment.duration(eventDuration.years(), 'years').humanize();
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
if (precision && eventDuration.months() > 0) {
|
|
|
|
durationString += ' ' + moment.duration(eventDuration.months(), 'months').humanize();
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
if (precision && eventDuration.days() > 0) {
|
|
|
|
durationString += ' ' + moment.duration(eventDuration.days(), 'days').humanize();
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
if (precision && eventDuration.hours() > 0) {
|
|
|
|
durationString += ' ' + moment.duration(eventDuration.hours(), 'hours').humanize();
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
if (precision && eventDuration.minutes() > 0) {
|
|
|
|
durationString += ' ' + moment.duration(eventDuration.minutes(), 'minutes').humanize();
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return durationString.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current timestamp in a "readable" format: YYYYMMDDHHmmSS.
|
|
|
|
*
|
|
|
|
* @return {string} The readable timestamp.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
readableTimestamp(): string {
|
2017-11-06 17:17:11 +01:00
|
|
|
return moment(Date.now()).format('YYYYMMDDHHmmSS');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current timestamp (UNIX format, seconds).
|
|
|
|
*
|
|
|
|
* @return {number} The current timestamp in seconds.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
timestamp(): number {
|
2017-11-06 17:17:11 +01:00
|
|
|
return Math.round(Date.now() / 1000);
|
|
|
|
}
|
|
|
|
|
2018-01-18 16:38:41 +01:00
|
|
|
/**
|
|
|
|
* Return the localized ISO format (i.e DDMMYY) from the localized moment format. Useful for translations.
|
|
|
|
*
|
2018-01-29 10:05:20 +01:00
|
|
|
* @param {any} localizedFormat Format to use.
|
2018-01-18 16:38:41 +01:00
|
|
|
* @return {string} Localized ISO format
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getLocalizedDateFormat(localizedFormat: any): string {
|
|
|
|
return moment.localeData().longDateFormat(localizedFormat);
|
2018-01-18 16:38:41 +01:00
|
|
|
}
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|