Class ProtoTimeUtils


  • @Internal
    public final class ProtoTimeUtils
    extends java.lang.Object
    Utility methods for handling protobuf/duration.proto and protobuf/timestamp.proto.

    Forked from com.google.protobuf.util package. These exist because there's not an equivalent util JAR published in maven central that's compatible with protolite. See relevant github issue.

    CEL Library Internals. Do Not Use.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static com.google.protobuf.Duration DURATION_ZERO
      A constant holding the duration of zero.
      static com.google.protobuf.Timestamp TIMESTAMP_EPOCH
      A constant holding the Timestamp of epoch time, 1970-01-01T00:00:00.000000000Z.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static com.google.protobuf.Duration add​(com.google.protobuf.Duration d1, com.google.protobuf.Duration d2)
      Adds two durations
      static com.google.protobuf.Timestamp add​(com.google.protobuf.Timestamp ts, com.google.protobuf.Duration dur)
      Adds two timestamps.
      static com.google.protobuf.Duration between​(com.google.protobuf.Timestamp from, com.google.protobuf.Timestamp to)
      Calculate the difference between two timestamps.
      static int compare​(com.google.protobuf.Duration x, com.google.protobuf.Duration y)
      Compares two durations.
      static int compare​(com.google.protobuf.Timestamp x, com.google.protobuf.Timestamp y)
      Compares two timestamps.
      static com.google.protobuf.Duration fromMillisToDuration​(long milliseconds)
      Create a Timestamp from the number of seconds elapsed from the epoch.
      static com.google.protobuf.Duration fromSecondsToDuration​(long seconds)
      Create a Timestamp from the number of seconds elapsed from the epoch.
      static com.google.protobuf.Timestamp fromSecondsToTimestamp​(long seconds)
      Create a Timestamp from the number of seconds elapsed from the epoch.
      static com.google.protobuf.Timestamp now()
      Create a Timestamp using the best-available (in terms of precision) system clock.
      static com.google.protobuf.Timestamp parse​(java.lang.String value)
      Parse from RFC 3339 date string to Timestamp.
      static com.google.protobuf.Duration subtract​(com.google.protobuf.Duration d1, com.google.protobuf.Duration d2)
      Subtract a duration from another.
      static com.google.protobuf.Timestamp subtract​(com.google.protobuf.Timestamp ts, com.google.protobuf.Duration dur)
      Subtracts two timestamps
      static long toHours​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of hours.
      static java.time.Duration toJavaDuration​(com.google.protobuf.Duration duration)
      Convert a Duration object to java-based Duration.
      static java.time.Instant toJavaInstant​(com.google.protobuf.Timestamp timestamp)
      Convert a Timestamp object to java-based Instant.
      static long toMillis​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of milliseconds.
      static long toMinutes​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of minutes.
      static com.google.protobuf.Duration toProtoDuration​(java.time.Duration duration)
      Convert a Duration object to proto-based Duration.
      static com.google.protobuf.Timestamp toProtoTimestamp​(java.time.Instant instant)
      Convert a Instant object to proto-based Timestamp.
      static long toSeconds​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of seconds.
      static long toSeconds​(com.google.protobuf.Timestamp timestamp)
      Convert a Timestamp to the number of seconds elapsed from the epoch.
      static java.lang.String toString​(com.google.protobuf.Duration duration)
      Convert Duration to string format.
      static java.lang.String toString​(com.google.protobuf.Timestamp timestamp)
      Convert Timestamp to RFC 3339 date string format.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TIMESTAMP_EPOCH

        public static final com.google.protobuf.Timestamp TIMESTAMP_EPOCH
        A constant holding the Timestamp of epoch time, 1970-01-01T00:00:00.000000000Z.
      • DURATION_ZERO

        public static final com.google.protobuf.Duration DURATION_ZERO
        A constant holding the duration of zero.
    • Method Detail

      • toProtoTimestamp

        public static com.google.protobuf.Timestamp toProtoTimestamp​(java.time.Instant instant)
        Convert a Instant object to proto-based Timestamp.
      • toProtoDuration

        public static com.google.protobuf.Duration toProtoDuration​(java.time.Duration duration)
        Convert a Duration object to proto-based Duration.
      • toJavaInstant

        public static java.time.Instant toJavaInstant​(com.google.protobuf.Timestamp timestamp)
        Convert a Timestamp object to java-based Instant.
      • toJavaDuration

        public static java.time.Duration toJavaDuration​(com.google.protobuf.Duration duration)
        Convert a Duration object to java-based Duration.
      • toSeconds

        public static long toSeconds​(com.google.protobuf.Timestamp timestamp)
        Convert a Timestamp to the number of seconds elapsed from the epoch.
      • toSeconds

        public static long toSeconds​(com.google.protobuf.Duration duration)
        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.
      • toHours

        public static long toHours​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of hours. The result will be rounded towards 0 to the nearest hour.
      • toMinutes

        public static long toMinutes​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of minutes. The result will be rounded towards 0 to the nearest minute.
      • toMillis

        public static long toMillis​(com.google.protobuf.Duration duration)
        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.
      • fromSecondsToTimestamp

        public static com.google.protobuf.Timestamp fromSecondsToTimestamp​(long seconds)
        Create a Timestamp from the number of seconds elapsed from the epoch.
      • fromSecondsToDuration

        public static com.google.protobuf.Duration fromSecondsToDuration​(long seconds)
        Create a Timestamp from the number of seconds elapsed from the epoch.
      • fromMillisToDuration

        public static com.google.protobuf.Duration fromMillisToDuration​(long milliseconds)
        Create a Timestamp from the number of seconds elapsed from the epoch.
      • toString

        public static java.lang.String toString​(com.google.protobuf.Timestamp timestamp)
        Convert Timestamp to RFC 3339 date string format. The output will always be Z-normalized and uses 0, 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

        Example of generated format: "1972-01-01T10:00:20.021Z"

        Returns:
        The string representation of the given timestamp.
        Throws:
        java.lang.IllegalArgumentException - if the given timestamp is not in the valid range.
      • toString

        public static java.lang.String toString​(com.google.protobuf.Duration duration)
        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).
        Returns:
        The string representation of the given duration.
        Throws:
        java.lang.IllegalArgumentException - if the given duration is not in the valid range.
      • parse

        public static com.google.protobuf.Timestamp parse​(java.lang.String value)
                                                   throws java.text.ParseException
        Parse from RFC 3339 date string to Timestamp. This method accepts all outputs of toString(Timestamp) and it also accepts any fractional digits (or none) and any offset as long as they fit into nano-seconds precision.

        Example of accepted format: "1972-01-01T10:00:20.021-05:00"

        Returns:
        a Timestamp parsed from the string
        Throws:
        java.text.ParseException - if parsing fails
      • add

        public static com.google.protobuf.Duration add​(com.google.protobuf.Duration d1,
                                                       com.google.protobuf.Duration d2)
        Adds two durations
      • add

        public static com.google.protobuf.Timestamp add​(com.google.protobuf.Timestamp ts,
                                                        com.google.protobuf.Duration dur)
        Adds two timestamps.
      • subtract

        public static com.google.protobuf.Duration subtract​(com.google.protobuf.Duration d1,
                                                            com.google.protobuf.Duration d2)
        Subtract a duration from another.
      • subtract

        public static com.google.protobuf.Timestamp subtract​(com.google.protobuf.Timestamp ts,
                                                             com.google.protobuf.Duration dur)
        Subtracts two timestamps
      • between

        public static com.google.protobuf.Duration between​(com.google.protobuf.Timestamp from,
                                                           com.google.protobuf.Timestamp to)
        Calculate the difference between two timestamps.
      • compare

        public static int compare​(com.google.protobuf.Duration x,
                                  com.google.protobuf.Duration y)
        Compares two durations. The value returned is identical to what would be returned by: Durations.comparator().compare(x, y).
        Returns:
        the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
      • compare

        public static int compare​(com.google.protobuf.Timestamp x,
                                  com.google.protobuf.Timestamp y)
        Compares two timestamps. The value returned is identical to what would be returned by: Timestamps.comparator().compare(x, y).
        Returns:
        the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
      • now

        public static com.google.protobuf.Timestamp now()
        Create a Timestamp using the best-available (in terms of precision) system clock.

        Note: that while this API is convenient, it may harm the testability of your code, as you're unable to mock the current time. Instead, you may want to consider injecting a clock instance to read the current time.