Skip to content

Commit 7e2c34c

Browse files
committed
server: fix parsing iso8601 durations with decimals in the seconds place
1 parent 06b4a6c commit 7e2c34c

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

server/services/parsing/iso8601.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* Examples: PT40M25S
44
*/
55
export function parseIso8601Duration(duration: string): number {
6-
let match = /P(\d+D)?(?:T(\d+H)?(\d+M)?(\d+S)?)?/
6+
let match = /P(\d+D)?(?:T(\d+H)?(\d+M)?([\d.]+S)?)?/
77
.exec(duration)
88
?.slice(1)
99
.map(x => {
1010
if (x !== null && x !== undefined) {
11-
return x.replace(/\D/, "");
11+
return x.replace(/[^\d.]/, "");
1212
}
1313
});
1414

@@ -19,7 +19,7 @@ export function parseIso8601Duration(duration: string): number {
1919
const days = parseInt(match[0] ?? "0", 10) || 0;
2020
const hours = parseInt(match[1] ?? "0", 10) || 0;
2121
const minutes = parseInt(match[2] ?? "0", 10) || 0;
22-
const seconds = parseInt(match[3] ?? "0", 10) || 0;
22+
const seconds = parseFloat(match[3] ?? "0") || 0;
2323

2424
return days * (24 * 3600) + hours * 3600 + minutes * 60 + seconds;
2525
}

server/tests/unit/services/parsing/iso8601.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { parseIso8601Duration } from "../../../../services/parsing/iso8601";
33
describe("parse iso8601 duration", () => {
44
it.each([
55
["PT10S", 10],
6+
["PT10.0S", 10],
7+
["PT10.000S", 10],
68
["PT5M", 5 * 60],
79
["PT40M25S", 40 * 60 + 25],
810
["PT1H", 1 * 60 * 60],

0 commit comments

Comments
 (0)