parseDurationString static method

({int nanos, BigInt seconds}) parseDurationString(
  1. Object? value
)

Parses the canonical "3.000001s" form into (seconds, nanos).

Implementation

static ({BigInt seconds, int nanos}) parseDurationString(Object? value) {
  if (value == null) {
    throw BuError(
      DefaultErrorType.invalidArgument,
      "Cannot parse null duration string.",
    );
  }
  var text = (value as String).trim();
  if (text.endsWith('s')) text = text.substring(0, text.length - 1);
  final negative = text.startsWith('-');
  if (negative) text = text.substring(1);
  final parts = text.split('.');
  final seconds = BigInt.parse(parts[0].isEmpty ? '0' : parts[0]);
  var nanos = 0;
  if (parts.length > 1) {
    nanos = int.parse(parts[1].padRight(9, '0').substring(0, 9));
  }
  return (
    seconds: negative ? -seconds : seconds,
    nanos: negative ? -nanos : nanos,
  );
}