Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-ins: time diff function #3348

Closed
andrehaland opened this issue Apr 6, 2021 · 3 comments · Fixed by #3358
Closed

Built-ins: time diff function #3348

andrehaland opened this issue Apr 6, 2021 · 3 comments · Fixed by #3358

Comments

@andrehaland
Copy link
Contributor

andrehaland commented Apr 6, 2021

There are some use cases for calculating the difference between two Time values (e.g. calculate time since last login, or calculate the age based on birthdate). Is this something that would be accepted as an addition to the time built-ins?

output := time.diff(a, b)

Example implementation can be seen from this SO post:
https://stackoverflow.com/a/36531443

@ashutosh-narkar
Copy link
Member

One way of doing this currently is something like https://play.openpolicyagent.org/p/ZDLf9k1fuC. The time-builtins use Go's time package. Feel free to add a new one !

@andrehaland
Copy link
Contributor Author

andrehaland commented Apr 7, 2021

One way of doing this currently is something like https://play.openpolicyagent.org/p/ZDLf9k1fuC

I should have been more specific. I was thinking of a difference on the format [years, months, days, hours, minutes, seconds]. The provided example only gives a duration, and when converting a duration to the wanted format we have to make some considerations, such as the number of days in a specific month etc., as described in the SO post:

https://stackoverflow.com/a/36531443

I am not sure if the required logic is possible to implement with Rego, so I will start on adding the new builtin. I was thinking of something like this:

./opa_darwin_amd64 eval 'diff := time.diff(time.parse_ns("2006-01-02", "2000-12-01"), time.now_ns())' --format=pretty
+-------------------+
|       diff        |
+-------------------+
| [20,4,6,10,10,21] |
+-------------------+

Any comments on this format @ashutosh-narkar?

@ashutosh-narkar
Copy link
Member

ashutosh-narkar commented Apr 8, 2021

This seems reasonable to me. Or even [hour, minute, second] which is what the Sub function in Go returns I believe should be fine as well.

andrehaland added a commit to andrehaland/opa that referenced this issue Apr 12, 2021
This built-in function makes it possible to get the absolute
difference between to unix timestamps (nanoseconds since epoch)
on the format [year, month, day, hour, minute, second].

Fixes: open-policy-agent#3348

Signed-off-by: Andre Håland <andre.haland@gmail.com>
srenatus pushed a commit that referenced this issue Apr 13, 2021
This built-in function makes it possible to get the absolute
difference between to unix timestamps (nanoseconds since epoch)
on the format [year, month, day, hour, minute, second].

Fixes: #3348

Signed-off-by: Andre Håland <andre.haland@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants