parseIso8601 static method

Duration parseIso8601(
  1. String? value
)

Implementation

static Duration parseIso8601(String? value) {
  if (value == null || value.isEmpty) return Duration.zero;
  final RegExpMatch? m = RegExp(r"^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?)?$").firstMatch(value.trim());
  if (m == null) return Duration.zero;
  final double seconds = double.tryParse(m.group(6) ?? "0") ?? 0;
  return Duration(
    days: (int.tryParse(m.group(3) ?? "0") ?? 0) + (int.tryParse(m.group(2) ?? "0") ?? 0) * 30 + (int.tryParse(m.group(1) ?? "0") ?? 0) * 365,
    hours: int.tryParse(m.group(4) ?? "0") ?? 0,
    minutes: int.tryParse(m.group(5) ?? "0") ?? 0,
    milliseconds: (seconds * 1000).round(),
  );
}