go-task / slim-sprig

Useful template functions for Go templates.
https://go-task.github.io/slim-sprig/
MIT License
62 stars 16 forks source link

Date functions limited to local timezone #18

Closed wburningham closed 6 months ago

wburningham commented 6 months ago

Context:

I'm using slim-sprig in as part of a go-task/task Taskfile. The end goal is to be able to parse a a git commit timestamp into a particular UTC format. I'm working with various versions of git so I can't rely on the commit timestamp formatting built into git. I also have a hard constraint on the final format being in UTC.

Problem:

From what I can tell all date formatting functions in the slim-sprig library (ie dateInZone) hard codes the local timezone. This means there is no way to run time.Time.Format using the UTC timezone.

Solution:

If my problem stated above is correct (I may be missing something in how to accomplish the desire goal so please let me know if I missed something), I'd like to propose a method to format a date in a given timezone (or just support UTC). This could be exposing the dateInZone function or a new dateInUTC or something else.

I'm happy to submit a PR, but I'd like to get feedback on the agreed upon problem and solution.

Thanks

andreynering commented 6 months ago

Hi @wburningham,

I didn't understand why dateInZone does not work for you, as I did a quick test and it seems to be working as expected:

version: '3'

tasks:
  date:
    cmds:
      - 'echo {{now | date "2006-01-02T15:04:05Z07:00"}}'
      - 'echo {{dateInZone "2006-01-02T15:04:05Z07:00" (now) "UTC"}}'

Hopefully that helps you to do what you need.


We don't usually add new functions to this fork because they should be directed to upstream.

That said, I just took a look, and the upstream repo is abandoned, and there is an attempt to have a maintained fork. See:

I opened an issue on the Task repo to track a possible migration to it in the future:

Just a FYI in case you find out another function is really needed. In that case, I think adding the function directly on the Task repo for now would possibly be the best option.

wburningham commented 6 months ago

I was not using dateInZone correctly. We can close out the issue.

Thanks for sharing those links.