Skip to main content

Rego Built-in Function: time.parse_ns

time.parse_ns is Rego's built-in function that parses a string into a timestamp in nanoseconds since the Unix epoch. This is useful converting input data to a format that can be compared with other timestamps or used in time calculations.

Examples

Check if a time is within a period

In order to check if a time is in a period of time, we need to know the start and the end of the period first. This policy defines two dates in time to mark the start and the end of the period, before testing if the supplied time is in the period.

policy.rego
package play

import rego.v1

# 2006-01-02 is the time format string for yyyy-mm-dd
start_date := time.parse_ns("2006-01-02", "1999-01-01")

end_date := time.parse_ns("2006-01-02", "2000-01-01")

parsed_time := time.parse_rfc3339_ns(input.time)

default allow := false

allow if {
parsed_time > start_date
parsed_time < end_date
}
input.json
{
"time": "1999-07-02T13:14:46.878235008Z"
}
data.json
{}

Open in OPA Playground

RuleOutput ValueNotes
allowtrueThe input time is in the year 1999
tip

See the format and clock built-in functions for examples for more uses of time.parse_ns().