Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: fix parsing iso8601 durations with decimals in the seconds place #1230

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/services/parsing/iso8601.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Examples: PT40M25S
*/
export function parseIso8601Duration(duration: string): number {
let match = /P(\d+D)?(?:T(\d+H)?(\d+M)?(\d+S)?)?/
let match = /P(\d+D)?(?:T(\d+H)?(\d+M)?([\d.]+S)?)?/
.exec(duration)
?.slice(1)
.map(x => {
if (x !== null && x !== undefined) {
return x.replace(/\D/, "");
return x.replace(/[^\d.]/, "");
}
});

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

return days * (24 * 3600) + hours * 3600 + minutes * 60 + seconds;
}
2 changes: 2 additions & 0 deletions server/tests/unit/services/parsing/iso8601.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { parseIso8601Duration } from "../../../../services/parsing/iso8601";
describe("parse iso8601 duration", () => {
it.each([
["PT10S", 10],
["PT10.0S", 10],
["PT10.000S", 10],
["PT5M", 5 * 60],
["PT40M25S", 40 * 60 + 25],
["PT1H", 1 * 60 * 60],
Expand Down
Loading