From ea685879bb19ea2ee787f52e7118b8641809c70e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 15 Dec 2023 09:54:08 +0100 Subject: [PATCH] Fix time to int parsing --- packages/core-utils/src/common/date.ts | 8 ++++---- packages/tests/src/server-helpers/core-utils.ts | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/core-utils/src/common/date.ts b/packages/core-utils/src/common/date.ts index 9f106945a..8cf5ac5db 100644 --- a/packages/core-utils/src/common/date.ts +++ b/packages/core-utils/src/common/date.ts @@ -52,13 +52,13 @@ function timeToInt (time: number | string) { if (typeof time === 'number') return time // Try with 00h00m00s format first - const reg = new RegExp(`^(\\d+h)?(\\d+m)?(\\d+)s?$`) + const reg = new RegExp(`^((?\\d+)h)?((?\\d+)m)?((?\\d+)s?)?$`) const matches = time.match(reg) if (matches) { - const hours = parseInt(matches[1] || '0', 10) - const minutes = parseInt(matches[2] || '0', 10) - const seconds = parseInt(matches[3] || '0', 10) + const hours = parseInt(matches.groups['hours'] || '0', 10) + const minutes = parseInt(matches.groups['minutes'] || '0', 10) + const seconds = parseInt(matches.groups['seconds'] || '0', 10) return hours * 3600 + minutes * 60 + seconds } diff --git a/packages/tests/src/server-helpers/core-utils.ts b/packages/tests/src/server-helpers/core-utils.ts index 43afab39a..627260d6b 100644 --- a/packages/tests/src/server-helpers/core-utils.ts +++ b/packages/tests/src/server-helpers/core-utils.ts @@ -78,6 +78,9 @@ describe('Time to int', function () { expect(timeToInt('02h02m03s')).to.equal(7323) expect(timeToInt('2:02:3')).to.equal(7323) + expect(timeToInt('5h10m')).to.equal(5 * 3600 + 60 * 10) + expect(timeToInt('5h10m0s')).to.equal(5 * 3600 + 60 * 10) + expect(timeToInt(3500)).to.equal(3500) }) })