/** * Convert a Duration to the number of seconds. The result will be rounded towards 0 to the * nearest second. E.g., if the duration represents -1 nanosecond, it will be rounded to 0. */ public static long toSeconds(Duration duration) { return checkValid(duration).getSeconds(); }
@Override public int compare(Duration d1, Duration d2) { checkValid(d1); checkValid(d2); int secDiff = Long.compare(d1.getSeconds(), d2.getSeconds()); return (secDiff != 0) ? secDiff : Integer.compare(d1.getNanos(), d2.getNanos()); } };
/** * Convert Duration to string format. The string format will contains 3, 6, or 9 fractional digits * depending on the precision required to represent the exact Duration value. For example: "1s", * "1.010s", "1.000000100s", "-3.100s" The range that can be represented by Duration is from * -315,576,000,000 to +315,576,000,000 inclusive (in seconds). * * @return The string representation of the given duration. * @throws IllegalArgumentException if the given duration is not in the valid range. */ public static String toString(Duration duration) { checkValid(duration); long seconds = duration.getSeconds(); int nanos = duration.getNanos(); StringBuilder result = new StringBuilder(); if (seconds < 0 || nanos < 0) { result.append("-"); seconds = -seconds; nanos = -nanos; } result.append(seconds); if (nanos != 0) { result.append("."); result.append(Timestamps.formatNanos(nanos)); } result.append("s"); return result.toString(); }
/** Subtract a duration from another. */ public static Duration subtract(Duration d1, Duration d2) { checkValid(d1); checkValid(d2); return normalizedDuration( checkedSubtract(d1.getSeconds(), d2.getSeconds()), checkedSubtract(d1.getNanos(), d2.getNanos())); }
/** Add two durations. */ public static Duration add(Duration d1, Duration d2) { checkValid(d1); checkValid(d2); return normalizedDuration( checkedAdd(d1.getSeconds(), d2.getSeconds()), checkedAdd(d1.getNanos(), d2.getNanos())); }
/** Convert a Duration to the number of nanoseconds. */ public static long toNanos(Duration duration) { checkValid(duration); return checkedAdd( checkedMultiply(duration.getSeconds(), NANOS_PER_SECOND), duration.getNanos()); }
/** * Convert a Duration to the number of microseconds. The result will be rounded towards 0 to the * nearest microseconds. E.g., if the duration represents -1 nanosecond, it will be rounded to 0. */ public static long toMicros(Duration duration) { checkValid(duration); return checkedAdd( checkedMultiply(duration.getSeconds(), MICROS_PER_SECOND), duration.getNanos() / NANOS_PER_MICROSECOND); }
/** * Convert a Duration to the number of milliseconds. The result will be rounded towards 0 to the * nearest millisecond. E.g., if the duration represents -1 nanosecond, it will be rounded to 0. */ public static long toMillis(Duration duration) { checkValid(duration); return checkedAdd( checkedMultiply(duration.getSeconds(), MILLIS_PER_SECOND), duration.getNanos() / NANOS_PER_MILLISECOND); }
static Duration normalizedDuration(long seconds, int nanos) { if (nanos <= -NANOS_PER_SECOND || nanos >= NANOS_PER_SECOND) { seconds = checkedAdd(seconds, nanos / NANOS_PER_SECOND); nanos %= NANOS_PER_SECOND; } if (seconds > 0 && nanos < 0) { nanos += NANOS_PER_SECOND; // no overflow since nanos is negative (and we're adding) seconds--; // no overflow since seconds is positive (and we're decrementing) } if (seconds < 0 && nanos > 0) { nanos -= NANOS_PER_SECOND; // no overflow since nanos is positive (and we're subtracting) seconds++; // no overflow since seconds is negative (and we're incrementing) } Duration duration = Duration.newBuilder().setSeconds(seconds).setNanos(nanos).build(); return checkValid(duration); } }
/** Subtract a duration from a timestamp. */ public static Timestamp subtract(Timestamp start, Duration length) { checkValid(start); Durations.checkValid(length); return normalizedTimestamp( checkedSubtract(start.getSeconds(), length.getSeconds()), checkedSubtract(start.getNanos(), length.getNanos())); }
/** Add a duration to a timestamp. */ public static Timestamp add(Timestamp start, Duration length) { checkValid(start); Durations.checkValid(length); return normalizedTimestamp( checkedAdd(start.getSeconds(), length.getSeconds()), checkedAdd(start.getNanos(), length.getNanos())); }