feat(parser): add ISO8601 duration parser

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 15:02:08 +02:00
parent 5b714919a0
commit c56201c5f3
2 changed files with 37 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
const PATTERN = /^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/;
export function parseIso8601Duration(input: string | null | undefined): number | null {
if (typeof input !== 'string' || input.length === 0) return null;
if (input === 'P' || input === 'PT') return null;
const m = PATTERN.exec(input);
if (!m) return null;
const [, d, h, min, s] = m;
if (!d && !h && !min && !s) return null;
const days = d ? parseInt(d, 10) : 0;
const hours = h ? parseInt(h, 10) : 0;
const mins = min ? parseInt(min, 10) : 0;
return days * 24 * 60 + hours * 60 + mins;
}