/** * Convert a Timestamp to the number of seconds elapsed from the epoch. * * <p>The result will be rounded down to the nearest second. E.g., if the timestamp represents * "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 second. */ public static long toSeconds(Timestamp timestamp) { return checkValid(timestamp).getSeconds(); }
@Override public int compare(Timestamp t1, Timestamp t2) { checkValid(t1); checkValid(t2); int secDiff = Long.compare(t1.getSeconds(), t2.getSeconds()); return (secDiff != 0) ? secDiff : Integer.compare(t1.getNanos(), t2.getNanos()); } };
/** * Convert Timestamp to RFC 3339 date string format. The output will always be Z-normalized and * uses 3, 6 or 9 fractional digits as required to represent the exact value. Note that Timestamp * can only represent time from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. See * https://www.ietf.org/rfc/rfc3339.txt * * <p>Example of generated format: "1972-01-01T10:00:20.021Z" * * @return The string representation of the given timestamp. * @throws IllegalArgumentException if the given timestamp is not in the valid range. */ public static String toString(Timestamp timestamp) { checkValid(timestamp); long seconds = timestamp.getSeconds(); int nanos = timestamp.getNanos(); StringBuilder result = new StringBuilder(); // Format the seconds part. Date date = new Date(seconds * MILLIS_PER_SECOND); result.append(timestampFormat.get().format(date)); // Format the nanos part. if (nanos != 0) { result.append("."); result.append(formatNanos(nanos)); } result.append("Z"); return result.toString(); }
/** Calculate the difference between two timestamps. */ public static Duration between(Timestamp from, Timestamp to) { checkValid(from); checkValid(to); return Durations.normalizedDuration( checkedSubtract(to.getSeconds(), from.getSeconds()), checkedSubtract(to.getNanos(), from.getNanos())); }
/** * Convert a Timestamp to the number of microseconds elapsed from the epoch. * * <p>The result will be rounded down to the nearest microsecond. E.g., if the timestamp * represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 microsecond. */ public static long toMicros(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( checkedMultiply(timestamp.getSeconds(), MICROS_PER_SECOND), timestamp.getNanos() / NANOS_PER_MICROSECOND); }
/** * Convert a Timestamp to the number of milliseconds elapsed from the epoch. * * <p>The result will be rounded down to the nearest millisecond. E.g., if the timestamp * represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 millisecond. */ public static long toMillis(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( checkedMultiply(timestamp.getSeconds(), MILLIS_PER_SECOND), timestamp.getNanos() / NANOS_PER_MILLISECOND); }
/** Convert a Timestamp to the number of nanoseconds elapsed from the epoch. */ public static long toNanos(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( checkedMultiply(timestamp.getSeconds(), NANOS_PER_SECOND), timestamp.getNanos()); }
static Timestamp normalizedTimestamp(long seconds, int nanos) { if (nanos <= -NANOS_PER_SECOND || nanos >= NANOS_PER_SECOND) { seconds = checkedAdd(seconds, nanos / NANOS_PER_SECOND); nanos = (int) (nanos % NANOS_PER_SECOND); } if (nanos < 0) { nanos = (int) (nanos + NANOS_PER_SECOND); // no overflow since nanos is negative (and we're adding) seconds = checkedSubtract(seconds, 1); } Timestamp timestamp = Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); return checkValid(timestamp); }
/** 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())); }