1818import static com .google .common .math .LongMath .checkedMultiply ;
1919import static com .google .common .math .LongMath .checkedSubtract ;
2020
21+ import com .google .common .annotations .VisibleForTesting ;
2122import com .google .common .base .Strings ;
2223import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2324import com .google .protobuf .Duration ;
4950public final class ProtoTimeUtils {
5051
5152 // Timestamp for "0001-01-01T00:00:00Z"
52- private static final long TIMESTAMP_SECONDS_MIN = - 62135596800L ;
53-
53+ @ VisibleForTesting
54+ static final long TIMESTAMP_SECONDS_MIN = - 62135596800L ;
5455 // Timestamp for "9999-12-31T23:59:59Z"
55- private static final long TIMESTAMP_SECONDS_MAX = 253402300799L ;
56- private static final long DURATION_SECONDS_MIN = -315576000000L ;
57- private static final long DURATION_SECONDS_MAX = 315576000000L ;
56+ @ VisibleForTesting
57+ static final long TIMESTAMP_SECONDS_MAX = 253402300799L ;
58+ @ VisibleForTesting
59+ static final long DURATION_SECONDS_MIN = -315576000000L ;
60+ @ VisibleForTesting
61+ static final long DURATION_SECONDS_MAX = 315576000000L ;
62+
5863 private static final int MILLIS_PER_SECOND = 1000 ;
5964
6065 private static final int NANOS_PER_SECOND = 1000000000 ;
@@ -344,7 +349,8 @@ public static Timestamp parse(String value) throws ParseException {
344349 }
345350 }
346351 try {
347- return normalizedTimestamp (seconds , nanos );
352+ Timestamp timestamp = normalizedTimestamp (seconds , nanos );
353+ return checkValid (timestamp );
348354 } catch (IllegalArgumentException e ) {
349355 ParseException ex =
350356 new ParseException (
@@ -532,8 +538,7 @@ private static Timestamp normalizedTimestamp(long seconds, int nanos) {
532538 nanos = nanos + NANOS_PER_SECOND ; // no overflow since nanos is negative (and we're adding)
533539 seconds = checkedSubtract (seconds , 1 );
534540 }
535- Timestamp timestamp = Timestamp .newBuilder ().setSeconds (seconds ).setNanos (nanos ).build ();
536- return checkValid (timestamp );
541+ return Timestamp .newBuilder ().setSeconds (seconds ).setNanos (nanos ).build ();
537542 }
538543
539544 private static Duration normalizedDuration (long seconds , int nanos ) {
@@ -549,8 +554,7 @@ private static Duration normalizedDuration(long seconds, int nanos) {
549554 nanos -= NANOS_PER_SECOND ; // no overflow since nanos is positive (and we're subtracting)
550555 seconds ++; // no overflow since seconds is negative (and we're incrementing)
551556 }
552- Duration duration = Duration .newBuilder ().setSeconds (seconds ).setNanos (nanos ).build ();
553- return checkValid (duration );
557+ return Duration .newBuilder ().setSeconds (seconds ).setNanos (nanos ).build ();
554558 }
555559
556560 private static String formatNanos (int nanos ) {
0 commit comments