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

Time Comparison Functions #372

Open
kevin-lindsay-1 opened this issue Jun 1, 2023 · 4 comments
Open

Time Comparison Functions #372

kevin-lindsay-1 opened this issue Jun 1, 2023 · 4 comments

Comments

@kevin-lindsay-1
Copy link

kevin-lindsay-1 commented Jun 1, 2023

Would be nice to have some time comparison functions.

For example, this doesn't work, and I don't see a way to do this using sprig as it stands, even though it provides some time support, albeit mostly formatting.

{{ $input := "2020-01-01" }}
{{ $inputDate := $input | toDate "2006-01-02" }}
{{ $currentDateString := now | date "2006-01-02" }}
{{ $currentDate := $currentDateString | toDate "2006-01-02" }}

{{ gt $currentDate $inputDate }}

this fails saying these types aren't comparable, and I don't see another function that can do this.

@kevin-lindsay-1 kevin-lindsay-1 changed the title Date Comparison Functions Time Comparison Functions Jun 1, 2023
@kevin-lindsay-1
Copy link
Author

I also think that the distinctions between dates, times, date strings, and unix time can easily get confusing in terms of naming conventions.

For example, I think now returns a time.Time, and when people refer to date they often mean either a date string, or aren't specific. I'm not sure if toDate is actually a time.Time under the hood, but if so I think that could get a bit confusing as currently written.

@kevin-lindsay-1
Copy link
Author

I think I might be able to do this by first converting the value to a unix epoch and performing the comparison that way. Not exactly what I originally intended to do, but it might be a good workaround.

@kevin-lindsay-1
Copy link
Author

I was able to work around this using unixEpoch and then converting that to int64 and then performing a comparison.

I still think that this should be included as a good utility function, but if a maintainer wants to "won't" this, feel free to close.

@isrealbm
Copy link

incompatible types for comparison error means the types of 2 sides are incorrect. Just parse both of them become the same data types like: {{if gt (now | unixEpoch | int64) .CreatedAt }}.

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

No branches or pull requests

2 participants