diff --git a/apps/client/src/common/utils/__tests__/dateConfig.test.js b/apps/client/src/common/utils/__tests__/dateConfig.test.js index 3c35ef70b6..40b5f031dc 100644 --- a/apps/client/src/common/utils/__tests__/dateConfig.test.js +++ b/apps/client/src/common/utils/__tests__/dateConfig.test.js @@ -45,6 +45,34 @@ describe('test forgivingStringToMillis()', () => { } }); + describe('#33 separators are parsed according to doc examples ', () => { + const ts = [ + { value: '0.1', expect: MILLIS_PER_MINUTE }, + { value: '0 1', expect: MILLIS_PER_MINUTE }, + { value: '0:1', expect: MILLIS_PER_MINUTE }, + { value: '0,1', expect: MILLIS_PER_MINUTE }, + { value: '2.2.2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND }, + { value: '2 2 2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND }, + { value: '2:2:2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND }, + { value: '2,2,2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND }, + { value: '2,2,2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND }, + { value: '10:', expect: 10 * MILLIS_PER_HOUR }, + { value: ':10', expect: 10 * MILLIS_PER_MINUTE }, + { value: '10', expect: 10 * MILLIS_PER_MINUTE }, + { value: '120', expect: MILLIS_PER_HOUR + 20 * MILLIS_PER_MINUTE }, + { value: '90m', expect: 90 * MILLIS_PER_MINUTE }, + { value: '1.2', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE }, + { value: '1.2.3', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 3 * MILLIS_PER_SECOND }, + { value: '123456', expect: 12 * MILLIS_PER_HOUR + 34 * MILLIS_PER_MINUTE + 56 * MILLIS_PER_SECOND }, + ]; + + for (const s of ts) { + it(`handles ${s.value}`, () => { + expect(forgivingStringToMillis(s.value)).toBe(s.expect); + }); + } + }); + describe('parses time strings', () => { const ts = [ { value: '1h2m3s', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 3 * MILLIS_PER_SECOND }, diff --git a/apps/client/src/common/utils/dateConfig.ts b/apps/client/src/common/utils/dateConfig.ts index 3738b96090..84a2d74d8e 100644 --- a/apps/client/src/common/utils/dateConfig.ts +++ b/apps/client/src/common/utils/dateConfig.ts @@ -97,12 +97,7 @@ function inferSeparators(value: string, isAM: boolean, isPM: boolean) { inferredMillis = parse(value[0]) * MILLIS_PER_HOUR + parse(value.substring(1)) * MILLIS_PER_MINUTE; } else if (length === 4) { inferredMillis = parse(value.substring(0, 2)) * MILLIS_PER_HOUR + parse(value.substring(2)) * MILLIS_PER_MINUTE; - } else if (length === 5) { - const hours = parse(value.substring(0, 2)); - const minutes = parse(value.substring(2, 4)); - const seconds = parse(value.substring(4)); - inferredMillis = hours * MILLIS_PER_HOUR + minutes * MILLIS_PER_MINUTE + seconds * MILLIS_PER_SECOND; - } else if (length >= 6) { + } else if (length >= 5) { const hours = parse(value.substring(0, 2)); const minutes = parse(value.substring(2, 4)); const seconds = parse(value.substring(4)); @@ -147,6 +142,7 @@ export const forgivingStringToMillis = (value: string): number => { hoursMatchValue = addAM; } if (first != null && second != null && third == null) { + // if string has two sections, treat as [hours] [minutes] millis = parse(first) * MILLIS_PER_HOUR; millis += parse(second) * MILLIS_PER_MINUTE; } @@ -155,6 +151,7 @@ export const forgivingStringToMillis = (value: string): number => { if (isPM && hoursMatchValue < 12) { millis += 12 * MILLIS_PER_HOUR; } + return millis; };