MOBILE-3585 pipe: Implement seconds to HMS pipe
parent
4bb7f0e97f
commit
3c634c4f6e
|
@ -16,6 +16,7 @@ import { NgModule } from '@angular/core';
|
||||||
import { CoreCreateLinksPipe } from './create-links.pipe';
|
import { CoreCreateLinksPipe } from './create-links.pipe';
|
||||||
import { CoreFormatDatePipe } from './format-date.pipe';
|
import { CoreFormatDatePipe } from './format-date.pipe';
|
||||||
import { CoreNoTagsPipe } from './no-tags.pipe';
|
import { CoreNoTagsPipe } from './no-tags.pipe';
|
||||||
|
import { CoreSecondsToHMSPipe } from './seconds-to-hms.pipe';
|
||||||
import { CoreTimeAgoPipe } from './time-ago.pipe';
|
import { CoreTimeAgoPipe } from './time-ago.pipe';
|
||||||
import { CoreBytesToSizePipe } from './bytes-to-size.pipe';
|
import { CoreBytesToSizePipe } from './bytes-to-size.pipe';
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ import { CoreBytesToSizePipe } from './bytes-to-size.pipe';
|
||||||
CoreTimeAgoPipe,
|
CoreTimeAgoPipe,
|
||||||
CoreFormatDatePipe,
|
CoreFormatDatePipe,
|
||||||
CoreBytesToSizePipe,
|
CoreBytesToSizePipe,
|
||||||
|
CoreSecondsToHMSPipe,
|
||||||
],
|
],
|
||||||
imports: [],
|
imports: [],
|
||||||
exports: [
|
exports: [
|
||||||
|
@ -34,6 +36,7 @@ import { CoreBytesToSizePipe } from './bytes-to-size.pipe';
|
||||||
CoreTimeAgoPipe,
|
CoreTimeAgoPipe,
|
||||||
CoreFormatDatePipe,
|
CoreFormatDatePipe,
|
||||||
CoreBytesToSizePipe,
|
CoreBytesToSizePipe,
|
||||||
|
CoreSecondsToHMSPipe,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class CorePipesModule {}
|
export class CorePipesModule {}
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
||||||
|
//
|
||||||
|
// 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 { Pipe, PipeTransform } from '@angular/core';
|
||||||
|
|
||||||
|
import { CoreTextUtils } from '@services/utils/text';
|
||||||
|
import { CoreLogger } from '@singletons/logger';
|
||||||
|
import { CoreConstants } from '@core/constants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pipe to convert a number of seconds to Hours:Minutes:Seconds.
|
||||||
|
*
|
||||||
|
* This converts a number of seconds to Hours:Minutes:Seconds. If the number of seconds is negative, returns 00:00:00.
|
||||||
|
*/
|
||||||
|
@Pipe({
|
||||||
|
name: 'coreSecondsToHMS',
|
||||||
|
})
|
||||||
|
export class CoreSecondsToHMSPipe implements PipeTransform {
|
||||||
|
|
||||||
|
protected logger: CoreLogger;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.logger = CoreLogger.getInstance('CoreSecondsToHMSPipe');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a number of seconds to Hours:Minutes:Seconds.
|
||||||
|
*
|
||||||
|
* @param seconds Number of seconds.
|
||||||
|
* @return Formatted seconds.
|
||||||
|
*/
|
||||||
|
transform(seconds: string | number): string {
|
||||||
|
if (!seconds || seconds < 0) {
|
||||||
|
seconds = 0;
|
||||||
|
} else if (typeof seconds == 'string') {
|
||||||
|
// Convert the value to a number.
|
||||||
|
const numberSeconds = parseInt(seconds, 10);
|
||||||
|
if (isNaN(numberSeconds)) {
|
||||||
|
this.logger.error('Invalid value received', seconds);
|
||||||
|
|
||||||
|
return seconds;
|
||||||
|
}
|
||||||
|
seconds = numberSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't allow decimals.
|
||||||
|
seconds = Math.floor(seconds);
|
||||||
|
|
||||||
|
const hours = Math.floor(seconds / CoreConstants.SECONDS_HOUR);
|
||||||
|
seconds -= hours * CoreConstants.SECONDS_HOUR;
|
||||||
|
const minutes = Math.floor(seconds / CoreConstants.SECONDS_MINUTE);
|
||||||
|
seconds -= minutes * CoreConstants.SECONDS_MINUTE;
|
||||||
|
|
||||||
|
return CoreTextUtils.instance.twoDigits(hours) + ':' + CoreTextUtils.instance.twoDigits(minutes) + ':' +
|
||||||
|
CoreTextUtils.instance.twoDigits(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue