mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-10 18:03:47 +00:00
63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import type { MaybeNumber } from 'ontime-types';
|
|
import { TimerType } from 'ontime-types';
|
|
|
|
export const MILLIS_PER_SECOND = 1000;
|
|
export const MILLIS_PER_MINUTE = 1000 * 60;
|
|
export const MILLIS_PER_HOUR = 1000 * 60 * 60;
|
|
|
|
export const dayInMs = 86400000;
|
|
export const maxDuration = dayInMs - MILLIS_PER_SECOND;
|
|
|
|
/**
|
|
* Converts value in milliseconds to seconds
|
|
* @param millis
|
|
* @returns
|
|
*/
|
|
export function millisToSeconds(
|
|
millis: MaybeNumber,
|
|
direction: TimerType.CountDown | TimerType.CountUp = TimerType.CountDown,
|
|
) {
|
|
if (millis === null) return 0;
|
|
|
|
if (direction === TimerType.CountDown) {
|
|
// 0 + is there to avoid result giving -0
|
|
return 0 + Math.ceil(millis / MILLIS_PER_SECOND);
|
|
}
|
|
|
|
if (direction === TimerType.CountUp) {
|
|
return Math.floor(millis / MILLIS_PER_SECOND);
|
|
}
|
|
|
|
throw new Error('?????');
|
|
}
|
|
|
|
/**
|
|
* Converts value in seconds to minutes
|
|
* @param seconds
|
|
* @returns
|
|
*/
|
|
export function secondsToMinutes(seconds: number): number {
|
|
return Math.floor(seconds / 60);
|
|
}
|
|
|
|
/**
|
|
* Converts value in seconds to hours
|
|
* @param seconds
|
|
* @returns
|
|
*/
|
|
export function secondsToHours(seconds: number): number {
|
|
return Math.floor(seconds / 3600);
|
|
}
|
|
|
|
/**
|
|
* @description Returns amount of seconds in a date given in milliseconds. For studio clock second indicator
|
|
* @param {MaybeNumber} millis time to format
|
|
* @returns amount of elapsed seconds
|
|
*/
|
|
export function secondsInMillis(millis: MaybeNumber): number {
|
|
if (!millis) {
|
|
return 0;
|
|
}
|
|
return Math.floor((millis % MILLIS_PER_MINUTE) / MILLIS_PER_SECOND);
|
|
}
|