google / cel-spec

Common Expression Language -- specification and binary representation
https://cel.dev
Apache License 2.0
2.83k stars 224 forks source link

[Question] how to specify now()? #283

Closed mrkagelui closed 1 year ago

mrkagelui commented 1 year ago

Hi, apologies if this is a wrong channel to ask questions, and please redirect me.

I couldn't seem to figure out how to specify the current time in CEL. the use case is, for example, I want to know if a user was registered 90 days ago. I'd pass the entire user info into the program, and I'd like to specify something like

user.registered_at - now() < duration("90 * 24h")

apparently that doesn't work. how should I do this?

TristonianJones commented 1 year ago

Hi @mrkagelui, thanks for the question.

It's much more common to register a variable named now (type timestamp) and provide an updated value for the variable on each evaluation. From CEL's perspective, a function like now() which fetches the latest timestamp would lead to subtle bugs related to differences in time between calls:

// Note, the duration calculation on the order of days needs to be modified as well
user.registered_at  - now < duration(string(90*24) + "h")

Hopefully, that answers your question.

-Tristan

mrkagelui commented 1 year ago

oh, sorry I'm a newbie here, let me try to spell this.

so in my use case, I'd need to somehow add the Now in my user struct, and then

user.registered_at - user.now < duration(string(90*24) + "h")

?

mrkagelui commented 1 year ago

I mean, I'm not sure how exactly to register a variable in every evaluation