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;
};