-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add duration formatter to the framework (#2177)
- Loading branch information
1 parent
4cc31d7
commit 09a66ed
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
}; |