googleDurationFromJson static method

({int nanos, BigInt seconds}) googleDurationFromJson(
  1. String value
)

Implementation

static ({BigInt seconds, int nanos}) googleDurationFromJson(String value) {
  final totalNanos = BigInt.tryParse(value);
  if (totalNanos != null) {
    final billion = BigInt.from(1000000000);
    return (
      seconds: totalNanos ~/ billion,
      nanos: (totalNanos % billion).toInt(),
    );
  }

  final match = RegExp(r'^(-)?(\d+)(?:\.(\d{1,9}))?s$').firstMatch(value);
  final negative = match?.group(1) != null;
  final seconds = BigInt.tryParse(match?.group(2) ?? "");
  if (match == null || seconds == null) {
    throw ArgumentException.invalidOperationArguments(
      "googleDurationFromJson",
      reason: "Invalid protobuf duration: $value",
    );
  }

  var nanos = 0;
  if (match.group(3) != null) {
    nanos = int.parse(match.group(3)!.padRight(9, '0'));
  }
  return (
    seconds: negative ? -seconds : seconds,
    nanos: negative ? -nanos : nanos,
  );
}