MCPcopy Create free account
hub / github.com/grpc/grpc-java / parseDuration

Method parseDuration

core/src/main/java/io/grpc/internal/JsonUtil.java:309–340  ·  view source on GitHub ↗

Parse from a string to produce a duration. Copy of com.google.protobuf.util.Durations#parse. @return A Duration parsed from the string. @throws ParseException if parsing fails.

(String value)

Source from the content-addressed store, hash-verified

307 * @throws ParseException if parsing fails.
308 */
309 private static long parseDuration(String value) throws ParseException {
310 // Must ended with "s".
311 if (value.isEmpty() || value.charAt(value.length() - 1) != 's') {
312 throw new ParseException("Invalid duration string: " + value, 0);
313 }
314 boolean negative = false;
315 if (value.charAt(0) == '-') {
316 negative = true;
317 value = value.substring(1);
318 }
319 String secondValue = value.substring(0, value.length() - 1);
320 String nanoValue = "";
321 int pointPosition = secondValue.indexOf('.');
322 if (pointPosition != -1) {
323 nanoValue = secondValue.substring(pointPosition + 1);
324 secondValue = secondValue.substring(0, pointPosition);
325 }
326 long seconds = Long.parseLong(secondValue);
327 int nanos = nanoValue.isEmpty() ? 0 : parseNanos(nanoValue);
328 if (seconds < 0) {
329 throw new ParseException("Invalid duration string: " + value, 0);
330 }
331 if (negative) {
332 seconds = -seconds;
333 nanos = -nanos;
334 }
335 try {
336 return normalizedDuration(seconds, nanos);
337 } catch (IllegalArgumentException e) {
338 throw new ParseException("Duration value is out of range.", 0);
339 }
340 }
341
342 /**
343 * Copy of {@link com.google.protobuf.util.Timestamps#parseNanos}.

Callers 1

getStringAsDurationMethod · 0.95

Calls 3

parseNanosMethod · 0.95
normalizedDurationMethod · 0.95
isEmptyMethod · 0.45

Tested by

no test coverage detected