Skip to content

Commit

Permalink
Add duration formatter to the framework (#2177)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboulais authored Oct 25, 2023
1 parent 4cc31d7 commit 09a66ed
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Framework/Frontend/js/src/formatter/formatTimeDuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import {splitDuration} from '../utilities/durationUtils.js';

/**
* Format a given duration (in milliseconds) in the format HH:MM:SS
*
* @param {number} duration the duration to format (in milliseconds)
*
* @return {string} the formatted duration
*/
export const formatTimeDuration = (duration) => {
if (duration !== 0 && !duration) {
return '-';
}

const {hours, minutes, seconds} = splitDuration(duration);

// eslint-disable-next-line require-jsdoc
const formatNumber = (number) => `${number}`.padStart(2, '0');

return `${formatNumber(hours)}:${formatNumber(minutes)}:${formatNumber(seconds)}`;
};
3 changes: 3 additions & 0 deletions Framework/Frontend/js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export {default as QueryRouter} from './QueryRouter.js';
// Utils
export {default as switchCase} from './switchCase.js';

// Formatters
export {formatTimeDuration} from "./formatter/formatTimeDuration.js";

// Singleton retrieving session data
export {default as sessionService} from './sessionService.js';

Expand Down
33 changes: 33 additions & 0 deletions Framework/Frontend/js/src/utilities/durationUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

const MILLISECONDS_IN_ONE_SECOND = 1000;

const MILLISECONDS_IN_ONE_MINUTE = 60 * MILLISECONDS_IN_ONE_SECOND;

const MILLISECONDS_IN_ONE_HOUR = 60 * MILLISECONDS_IN_ONE_MINUTE;

/**
* Returns the amount of hours, minutes and seconds contained in a given duration
*
* @param {number} duration the duration to split (in milliseconds)
*
* @return {{hours: number, seconds: number, minutes: number}} the amount of hours, minutes and seconds
*/
export const splitDuration = (duration) => {
const hours = Math.floor(duration / MILLISECONDS_IN_ONE_HOUR);
const minutes = Math.floor(duration % MILLISECONDS_IN_ONE_HOUR / MILLISECONDS_IN_ONE_MINUTE);
const seconds = Math.floor(duration % MILLISECONDS_IN_ONE_MINUTE / MILLISECONDS_IN_ONE_SECOND);

return {hours, minutes, seconds};
};

0 comments on commit 09a66ed

Please sign in to comment.