googleDurationToJson static method
Converts a protobuf Duration to its JSON representation.
Examples: seconds=3, nanos=0 -> "3s" seconds=3, nanos=1 -> "3.000000001s" seconds=3, nanos=1000 -> "3.000001s" seconds=-3, nanos=-500000000 -> "-3.5s"
Implementation
static String googleDurationToJson(
BigInt seconds,
int nanos, {
bool asNanos = false,
}) {
if (asNanos) {
final billion = BigInt.from(1000000000);
final totalNanos = seconds * billion + BigInt.from(nanos);
return totalNanos.toString();
}
if (nanos == 0) {
return '${seconds}s';
}
final fraction = nanos.abs().toString().padLeft(9, '0');
final trimmed = fraction.replaceFirst(RegExp(r'0+$'), '');
return '$seconds.$trimmed'
's';
}