Skip to main content

Rego Built-in Function: time.now_ns

time.now_ns is Rego's built-in function that returns the current time in nanoseconds since the Unix epoch. This is useful for comparing timestamps or calculating time differences.


Check if a time is in the past

In this example, we see compare an RFC3339 timestamp with the current time to determine if the timestamp is in the past.

If you have a time in a different format, you might want to use time.parse_ns function to convert it to nanoseconds before comparing it with the current time.

Observe that in Rego, comparing time can be done using the < and > operators just like comparing numbers and times in many other languages.

# policy.rego
parsed_time := time.parse_rfc3339_ns(input.time)

in_past if parsed_time < time.now_ns()
# input.json
"time": "2024-07-02T13:14:46.878235008Z"

Run in OPA Playground

RuleOutput Value