Package dev.cel.common.internal
Class ProtoTimeUtils
- java.lang.Object
-
- dev.cel.common.internal.ProtoTimeUtils
-
@Internal public final class ProtoTimeUtils extends java.lang.ObjectUtility methods for handlingprotobuf/duration.protoandprotobuf/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.DurationDURATION_ZEROA constant holding the duration of zero.static com.google.protobuf.TimestampTIMESTAMP_EPOCHA constant holding theTimestampof 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.Durationadd(com.google.protobuf.Duration d1, com.google.protobuf.Duration d2)Adds two durationsstatic com.google.protobuf.Timestampadd(com.google.protobuf.Timestamp ts, com.google.protobuf.Duration dur)Adds two timestamps.static com.google.protobuf.Durationbetween(com.google.protobuf.Timestamp from, com.google.protobuf.Timestamp to)Calculate the difference between two timestamps.static intcompare(com.google.protobuf.Duration x, com.google.protobuf.Duration y)Compares two durations.static intcompare(com.google.protobuf.Timestamp x, com.google.protobuf.Timestamp y)Compares two timestamps.static com.google.protobuf.DurationfromMillisToDuration(long milliseconds)Create a Timestamp from the number of seconds elapsed from the epoch.static com.google.protobuf.DurationfromSecondsToDuration(long seconds)Create a Timestamp from the number of seconds elapsed from the epoch.static com.google.protobuf.TimestampfromSecondsToTimestamp(long seconds)Create a Timestamp from the number of seconds elapsed from the epoch.static com.google.protobuf.Timestampnow()Create aTimestampusing the best-available (in terms of precision) system clock.static com.google.protobuf.Timestampparse(java.lang.String value)Parse from RFC 3339 date string to Timestamp.static com.google.protobuf.Durationsubtract(com.google.protobuf.Duration d1, com.google.protobuf.Duration d2)Subtract a duration from another.static com.google.protobuf.Timestampsubtract(com.google.protobuf.Timestamp ts, com.google.protobuf.Duration dur)Subtracts two timestampsstatic longtoHours(com.google.protobuf.Duration duration)Convert a Duration to the number of hours.static java.time.DurationtoJavaDuration(com.google.protobuf.Duration duration)Convert aDurationobject to java-basedDuration.static java.time.InstanttoJavaInstant(com.google.protobuf.Timestamp timestamp)Convert aTimestampobject to java-basedInstant.static longtoMillis(com.google.protobuf.Duration duration)Convert a Duration to the number of milliseconds.static longtoMinutes(com.google.protobuf.Duration duration)Convert a Duration to the number of minutes.static com.google.protobuf.DurationtoProtoDuration(java.time.Duration duration)Convert aDurationobject to proto-basedDuration.static com.google.protobuf.TimestamptoProtoTimestamp(java.time.Instant instant)Convert aInstantobject to proto-basedTimestamp.static longtoSeconds(com.google.protobuf.Duration duration)Convert a Duration to the number of seconds.static longtoSeconds(com.google.protobuf.Timestamp timestamp)Convert a Timestamp to the number of seconds elapsed from the epoch.static java.lang.StringtoString(com.google.protobuf.Duration duration)Convert Duration to string format.static java.lang.StringtoString(com.google.protobuf.Timestamp timestamp)Convert Timestamp to RFC 3339 date string format.
-
-
-
Method Detail
-
toProtoTimestamp
public static com.google.protobuf.Timestamp toProtoTimestamp(java.time.Instant instant)
Convert aInstantobject to proto-basedTimestamp.
-
toProtoDuration
public static com.google.protobuf.Duration toProtoDuration(java.time.Duration duration)
Convert aDurationobject to proto-basedDuration.
-
toJavaInstant
public static java.time.Instant toJavaInstant(com.google.protobuf.Timestamp timestamp)
Convert aTimestampobject to java-basedInstant.
-
toJavaDuration
public static java.time.Duration toJavaDuration(com.google.protobuf.Duration duration)
Convert aDurationobject to java-basedDuration.
-
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.txtExample 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.ParseExceptionParse from RFC 3339 date string to Timestamp. This method accepts all outputs oftoString(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
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > 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
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y
-
now
public static com.google.protobuf.Timestamp now()
Create aTimestampusing 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.
-
-