Time#
CEL supports timestamps and durations for working with time.
These correspond to google.protobuf.Timestamp and google.protobuf.Duration.
Timestamps#
Create a timestamp from an RFC 3339 string using the timestamp() function.
timestamp("2024-01-15T10:30:45.123Z")
// result: 2024-01-15T10:30:45.123Z (google.protobuf.Timestamp)
Timestamp components#
Extract date and time components from a timestamp. All methods return values in UTC by default.
Days and month components are zero-indexed (0 = Sunday/January).
// getDayOfWeek() is zero-indexed: Monday, January 15, 2024 returns 1.
timestamp("2024-01-15T10:30:45.123Z").getDayOfWeek()
// result: 1 (int)
// getDayOfMonth() is zero-indexed: January 15th returns 14.
timestamp("2024-01-15T10:30:45.123Z").getDayOfMonth()
// result: 14 (int)
// getDayOfYear() is zero-indexed: January 15th returns 14.
timestamp("2024-01-15T10:30:45.123Z").getDayOfYear()
// result: 14 (int)
// getMonth() is zero-indexed: January returns 0.
timestamp("2024-01-15T10:30:45.123Z").getMonth()
// result: 0 (int)
Timezones#
Pass a timezone string to extract components in a specific timezone. Timezones use IANA timezone names.
All component extraction methods accept an optional timezone argument:
getFullYear(), getMonth(), getDayOfMonth(), getDayOfWeek(), getDayOfYear(),
getHours(), getMinutes(), and getSeconds().
Invalid timezone names produce a runtime error.
timestamp("2024-01-15T10:30:45.123Z").getHours("Invalid/Timezone")
// error: unknown time zone Invalid/Timezone
Durations#
Create a duration from a string with unit suffixes using the duration() function.
Supported units: h (hours), m (minutes), s (seconds), ms (milliseconds), us (microseconds), ns (nanoseconds).
Duration components#
Extract the total value in a specific unit.
Values smaller than the requested unit return zero.
Arithmetic#
Add or subtract a duration from a timestamp.
timestamp("2024-01-15T10:30:45.123Z") + duration("1h")
// result: 2024-01-15T11:30:45.123Z (google.protobuf.Timestamp)
timestamp("2024-01-15T10:30:45.123Z") - duration("30m")
// result: 2024-01-15T10:00:45.123Z (google.protobuf.Timestamp)
Subtract two timestamps to get a duration.
timestamp("2024-01-15T11:30:45.123Z") - timestamp("2024-01-15T10:30:45.123Z")
// result: 1h0m0s (google.protobuf.Duration)
Add or subtract durations.
Numeric conversions#
Convert a timestamp to Unix epoch seconds.
Convert a duration to nanoseconds.
See also#
- Well-known types - Wrapper types, Struct, and Value
- Comparison operators - Ordering timestamps and durations
- Arithmetic operators - Time arithmetic