liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Possibility to add transformation of Int & Strings into Dates (time.Time)

Open GauthierHacout opened this issue 9 months ago • 0 comments

⚠ The ISSUE_TEMPLATE.md doesn't seem to work anymore, I had to manually copy the contents of the file to fill this issue ⚠

Checklist

  • [ X] I have searched the issue list
  • [ X] I have tested my example against Shopify Liquid. (Tested against Liquid JS implementation & Shopify Liquid implementation)

Version

I'm using v1.6.0

Expected Behavior

In other liquid library there's possibility to transform integer (and even string if they're in the right format) into date by considering them as UNIX timestamp.

For example:

{{ '1743099558' | date: '%y-%m-%d'}}
{{ 1743099558 | date: '%y-%m-%d'}}

Will both result in 2025-03-27 (today's date)

Which is very interesting because it allows do to some calculus and manipulation on dates easily, for example you can display the date of yesterday like this:

{{ 'now' | date: '%s' | minus: 86400 | date: '%y-%m-%d' }}

Do you think this feature could be added ?

Actual Behavior

Liquid error: can't convert int(1743096453) to type time.Time in {{ 1743096453 | date: "%y" }}

Possible Solution

I believe it would require modification here (https://github.com/osteele/liquid/blob/main/values/convert.go#L88) to create a new func allowing Int to be transformed into time.Time with the standard time.Unix func.

ParseDate (https://github.com/osteele/liquid/blob/main/values/parsedate.go#L48) could also be modified to check for on last transformation before returning an error. Transforming the string into an int and trying to apply time.Unix on it for example.

I am willing to work on this and create a PR if needed, I just wanted confirmation before that it was indeed something that would make sense.

GauthierHacout avatar Mar 27 '25 18:03 GauthierHacout