refactor: unify time constants

This commit is contained in:
Carlos Valente
2024-05-19 13:19:04 +02:00
committed by Carlos Valente
parent 119e6caca3
commit d05cc88748
7 changed files with 20 additions and 33 deletions
+12 -16
View File
@@ -1,10 +1,6 @@
import { MILLIS_PER_MINUTE } from 'ontime-utils';
import { MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND } from 'ontime-utils';
import { isISO8601 } from '../../../../packages/utils/src/date-utils/isTimeString.js';
const mts = 1000; // millis to seconds
const mtm = 1000 * 60; // millis to minutes
const mth = 1000 * 60 * 60; // millis to hours
export const timeFormat = 'HH:mm';
export const timeFormatSeconds = 'HH:mm:ss';
@@ -20,7 +16,7 @@ export const dateToMillis = (date: Date): number => {
const m = date.getMinutes();
const s = date.getSeconds();
return h * mth + m * mtm + s * mts;
return h * MILLIS_PER_HOUR + m * MILLIS_PER_MINUTE + s * MILLIS_PER_SECOND;
};
/**
@@ -60,7 +56,7 @@ export const forgivingStringToMillis = (value: string): number => {
//if past noon indicated add 12 hours
if (pastNoon) {
millis = mth * 12;
millis = MILLIS_PER_HOUR * 12;
}
// split string at known separators : , .
const separatorRegex = /[\s,:.]+/;
@@ -68,9 +64,9 @@ export const forgivingStringToMillis = (value: string): number => {
if (first != null && second != null && third != null) {
// if string has three sections, treat as [hours] [minutes] [seconds]
millis += parse(first) * mth;
millis += parse(second) * mtm;
millis += parse(third) * mts;
millis += parse(first) * MILLIS_PER_HOUR;
millis += parse(second) * MILLIS_PER_MINUTE;
millis += parse(third) * MILLIS_PER_SECOND;
} else if (first != null && second == null && third == null) {
// if string has one section,
// could be a complete string like 121010 - 12:10:10
@@ -78,18 +74,18 @@ export const forgivingStringToMillis = (value: string): number => {
const hours = first.substring(0, 2);
const minutes = first.substring(2, 4);
const seconds = first.substring(4);
millis += parse(hours) * mth;
millis += parse(minutes) * mtm;
millis += parse(seconds) * mts;
millis += parse(hours) * MILLIS_PER_HOUR;
millis += parse(minutes) * MILLIS_PER_MINUTE;
millis += parse(seconds) * MILLIS_PER_SECOND;
} else {
// otherwise lets treat as [minutes]
millis += parse(first) * mtm;
millis += parse(first) * MILLIS_PER_MINUTE;
}
}
if (first != null && second != null && third == null) {
// if string has two sections treat as [hours] [minutes]
millis += parse(first) * mth;
millis += parse(second) * mtm;
millis += parse(first) * MILLIS_PER_HOUR;
millis += parse(second) * MILLIS_PER_MINUTE;
}
return millis;
};
+2 -4
View File
@@ -26,8 +26,9 @@ export {
swapEventData,
} from './src/rundown-utils/rundownUtils.js';
// format utils
// time format utils
export {
dayInMs,
MILLIS_PER_HOUR,
MILLIS_PER_MINUTE,
MILLIS_PER_SECOND,
@@ -46,9 +47,6 @@ export {
export { isAlphanumeric } from './src/regex-utils/isAlphanumeric.js';
export { isColourHex } from './src/regex-utils/isColourHex.js';
// time utils
export { dayInMs, mts } from './src/timeConstants.js';
// helpers from externals
export { deepmerge } from './src/externals/deepmerge.js';
@@ -4,6 +4,9 @@ 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;
function convertMillis(millis: MaybeNumber, conversion: number) {
if (!millis) {
return 0;
@@ -1,5 +1,4 @@
import { dayInMs } from '../timeConstants';
import { MILLIS_PER_HOUR } from './conversionUtils';
import { dayInMs, MILLIS_PER_HOUR } from './conversionUtils';
import { formatFromMillis, millisToString, removeLeadingZero } from './timeFormatting';
describe('millisToString()', () => {
-9
View File
@@ -1,9 +0,0 @@
/**
* Milliseconds in a second
*/
export const mts = 1000;
/**
* Milliseconds in a day
*/
export const dayInMs = 86400000;
@@ -1,7 +1,7 @@
import type { OntimeEvent } from 'ontime-types';
import { TimeStrategy } from 'ontime-types';
import { dayInMs } from '../timeConstants';
import { dayInMs } from '../date-utils/conversionUtils';
import { calculateDuration, getLinkedTimes, validateTimes } from './validateTimes';
describe('validateTimes()', () => {
@@ -1,7 +1,7 @@
import type { OntimeEvent } from 'ontime-types';
import { TimeStrategy } from 'ontime-types';
import { dayInMs } from '../timeConstants.js';
import { dayInMs } from '../date-utils/conversionUtils.js';
import { validateTimeStrategy } from '../validate-events/validateEvent.js';
export function getLinkedTimes(