parseTimeout static method

Duration? parseTimeout(
  1. String? text
)

Decoded grpc-timeout, or null when malformed/absent.

Implementation

static Duration? parseTimeout(String? text) {
  if (text == null || text.length < 2 || text.length > 9) return null;
  final digits = text.substring(0, text.length - 1);
  final n = int.tryParse(digits);
  if (n == null) return null;
  switch (text[text.length - 1]) {
    case 'H':
      return Duration(hours: n);
    case 'M':
      return Duration(minutes: n);
    case 'S':
      return Duration(seconds: n);
    case 'm':
      return Duration(milliseconds: n);
    case 'u':
      return Duration(microseconds: n);
    case 'n':
      return Duration(microseconds: (n + 999) ~/ 1000);
    default:
      return null;
  }
}