Time
Function | Description | Meta |
---|---|---|
time.add_date |
Returns the nanoseconds since epoch after adding years, months and days to nanoseconds. Month & day values outside their usual ranges after the operation and will be normalized - for example, October 32 would become November 1. Arguments: Returns:ns (number)nanoseconds since the epoch years (number)number of years to add months (number)number of months to add days (number)number of days to add output (number)nanoseconds since the epoch representing the input time, with years, months and days added | v0.19.0 SDK-dependent |
time.clock |
Returns the Arguments: Returns:x (any<number, array<number, string>>)a number representing the nanoseconds since the epoch (UTC); or a two-element array of the nanoseconds, and a timezone string output (array<number, number, number>)the | SDK-dependent |
time.date |
Returns the Arguments: Returns:x (any<number, array<number, string>>)a number representing the nanoseconds since the epoch (UTC); or a two-element array of the nanoseconds, and a timezone string date (array<number, number, number>)an array of | SDK-dependent |
time.diff |
Returns the difference between two unix timestamps in nanoseconds (with optional timezone strings). Arguments: Returns:ns1 (any<number, array<number, string>>)nanoseconds since the epoch; or a two-element array of the nanoseconds, and a timezone string ns2 (any<number, array<number, string>>)nanoseconds since the epoch; or a two-element array of the nanoseconds, and a timezone string output (array<number, number, number, number, number, number>)difference between | v0.28.0 SDK-dependent |
time.format |
Returns the formatted timestamp for the nanoseconds since epoch. Arguments: Returns:x (any<number, array<number, string>, array<number, string, string>>)a number representing the nanoseconds since the epoch (UTC); or a two-element array of the nanoseconds, and a timezone string; or a three-element array of ns, timezone string and a layout string or golang defined formatting constant (see golang supported time formats) formatted timestamp (string)the formatted timestamp represented for the nanoseconds since the epoch in the supplied timezone (or UTC) | v0.48.0 SDK-dependent |
time.now_ns |
Returns the current time since epoch in nanoseconds. Returns:now (number)nanoseconds since epoch | SDK-dependent |
time.parse_duration_ns |
Returns the duration in nanoseconds represented by a string. Arguments: Returns:duration (string)a duration like "3m"; see the Go ns (number)the | SDK-dependent |
time.parse_ns |
Returns the time in nanoseconds parsed from the string in the given format. Arguments: Returns:layout (string)format used for parsing, see the Go value (string)input to parse according to ns (number)
| SDK-dependent |
time.parse_rfc3339_ns |
Returns the time in nanoseconds parsed from the string in RFC3339 format. Arguments: Returns:value (string)input string to parse in RFC3339 format ns (number)
| SDK-dependent |
time.weekday |
Returns the day of the week (Monday, Tuesday, ...) for the nanoseconds since epoch. Arguments: Returns:x (any<number, array<number, string>>)a number representing the nanoseconds since the epoch (UTC); or a two-element array of the nanoseconds, and a timezone string day (string)the weekday represented by | SDK-dependent |
Multiple calls to the time.now_ns
built-in function within a single policy
evaluation query will always return the same value.
Timezones can be specified as
- an IANA Time Zone string e.g. "America/New_York"
- "UTC" or "", which are equivalent to not passing a timezone (i.e. will return as UTC)
- "Local", which will use the local timezone.
Note that OPA will use the time/tzdata
data if none is present on the runtime filesystem (see the
Go time.LoadLocation()
documentation for more information).
Timestamp Parsing
OPA can parse timestamps of nearly arbitrary formats, and currently accepts the same inputs as Go's time.Parse()
utility.
As a result, either you will pass a supported constant, or you must describe the format of your timestamps using the Reference Timestamp that Go's time
module expects:
2006-01-02T15:04:05Z07:00
In other date formats, that same value is rendered as:
- January 2, 15:04:05, 2006, in time zone seven hours west of GMT
- Unix time:
1136239445
- Unix
date
command output:Mon Jan 2 15:04:05 MST 2006
- RFC3339 timestamp:
2006-01-02T15:04:05Z07:00
Examples of valid values for each timestamp field:
- Year:
"2006"
"06"
- Month:
"Jan"
"January"
"01"
"1"
- Day of the week:
"Mon"
"Monday"
- Day of the month:
"2"
"_2"
"02"
- Day of the year:
"__2"
"002"
- Hour:
"15"
"3"
"03"
(PM or AM) - Minute:
"4"
"04"
- Second:
"5"
"05"
- AM/PM mark:
"PM"
For supported constants, formatting of nanoseconds, time zones, and other fields, see the Go time/format
module documentation.
Timestamp Parsing
In OPA, we can parse a simple YYYY-MM-DD timestamp as follows:
"{}"
"{}"
package time_format
ts := "1985-10-27"
result := time.parse_ns("2006-01-02", ts)