From 3c634c4f6e0925e4631061446aa6de5a0366d541 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 6 Nov 2020 15:26:20 +0100 Subject: [PATCH] MOBILE-3585 pipe: Implement seconds to HMS pipe --- src/app/pipes/pipes.module.ts | 3 ++ src/app/pipes/seconds-to-hms.pipe.ts | 69 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/app/pipes/seconds-to-hms.pipe.ts diff --git a/src/app/pipes/pipes.module.ts b/src/app/pipes/pipes.module.ts index 492bf7e08..ed1d765d8 100644 --- a/src/app/pipes/pipes.module.ts +++ b/src/app/pipes/pipes.module.ts @@ -16,6 +16,7 @@ import { NgModule } from '@angular/core'; import { CoreCreateLinksPipe } from './create-links.pipe'; import { CoreFormatDatePipe } from './format-date.pipe'; import { CoreNoTagsPipe } from './no-tags.pipe'; +import { CoreSecondsToHMSPipe } from './seconds-to-hms.pipe'; import { CoreTimeAgoPipe } from './time-ago.pipe'; import { CoreBytesToSizePipe } from './bytes-to-size.pipe'; @@ -26,6 +27,7 @@ import { CoreBytesToSizePipe } from './bytes-to-size.pipe'; CoreTimeAgoPipe, CoreFormatDatePipe, CoreBytesToSizePipe, + CoreSecondsToHMSPipe, ], imports: [], exports: [ @@ -34,6 +36,7 @@ import { CoreBytesToSizePipe } from './bytes-to-size.pipe'; CoreTimeAgoPipe, CoreFormatDatePipe, CoreBytesToSizePipe, + CoreSecondsToHMSPipe, ], }) export class CorePipesModule {} diff --git a/src/app/pipes/seconds-to-hms.pipe.ts b/src/app/pipes/seconds-to-hms.pipe.ts new file mode 100644 index 000000000..f483237f0 --- /dev/null +++ b/src/app/pipes/seconds-to-hms.pipe.ts @@ -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); + } + +}