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

Order matters in date time comparisons #1537

Closed
djmcgreal-cc opened this issue Jan 30, 2023 · 5 comments
Closed

Order matters in date time comparisons #1537

djmcgreal-cc opened this issue Jan 30, 2023 · 5 comments
Labels

Comments

@djmcgreal-cc
Copy link

djmcgreal-cc commented Jan 30, 2023

Describe the bug

I had a date come in as a string, e.g. Expiration: "2023-01-30T15:53:09Z" that I wanted to check against now. I found that it only worked if I put now as the first argument, e.g. now < .Expiration works but .Expiration > now fails with

!!str not yet supported for comparison

Before I found that the ordering matters, I had tried to

  • force the str to be a timestamp with .Expiration tag="timestamp", but that resulted in the same error (although it was tagged in the output)
  • remove the "s, but I couldn't work out how to do that with style, ="" didn't change the output any

Version of yq: 4.29.2
Operating system: mac
Installed via: homebrew

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data1.yml:

Expiration: "2023-01-30T15:53:09Z"

Command
The command you ran:

yq -e '.Expiration > now'

Actual behavior

Error: !!str not yet supported for comparison

Expected behavior

true

(or false...)

@mikefarah
Copy link
Owner

Because you have quotes around the timestamp, yq thinks it's a string. If you remove the quotes, then it will have the correct type (timestamp).

It works the otherway around, because it knows now is a timestamp, so it tries to parse the 2nd arg as a timestamp as well...

@djmcgreal-cc
Copy link
Author

djmcgreal-cc commented Jan 31, 2023 via email

@mikefarah
Copy link
Owner

Actually, thinking about it more, I'll update the comparison logic to work both ways :)

To remove the quotes, you have to let it know that it's not a !!str but a !!timestamp. Easiest way to do that is to parse the value:

yq '.Expiration |= from_yaml'

This is effectively the same thing if you had a number in a string and wanted an actual number.

@djmcgreal-cc
Copy link
Author

djmcgreal-cc commented Feb 2, 2023 via email

@mikefarah
Copy link
Owner

Fixed in v4.31.1.

Generally yq compares against the type of the filed, so if the tag is number, is will compare as number, timestamp it will compare as that. There are now some extra smarts around time - but I think these smarts make it behave more intuitively. (IMO) it's unlikely you'd want to sort by string a timefield. That said you could still force it by providing a mismatching datetime formatting layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants