Masterminds / sprig

Useful template functions for Go templates.
http://masterminds.github.io/sprig/
MIT License
4.07k stars 423 forks source link

Parse Seconds from Duration #373

Open kevin-lindsay-1 opened 1 year ago

kevin-lindsay-1 commented 1 year ago

Attempting to simplify some templates, and currently I have a section that needs to create a duration and perform some math on it, and output certain fields as seconds. As a result, I need to have my template specify a duration in hours, minutes, and seconds. Once done, I then multiply those to get seconds, and then convert that duration.

Instead, it would be nice to be able to specify a duration and change it.

Current way to have to do this: Link to playground

Desired way to do this: Link to playground

jouve commented 9 months ago

you could play tricks with dateModify+unixEpoch

{{- $now := now -}}
{{ sub ($now | dateModify "1h35m8s" | dateModify "-10s" | unixEpoch) ($now | unixEpoch) | duration }}
42atomys commented 1 month ago

Hello everyone 👋,

I wanted to let you know that this issue has been addressed in the fork of this project at go-sprout/sprout. The fix has been implemented starting from version v0.3.0.

With new function toDuration introduced in v0.3.0, the issue should be resolved.

The function is used to convert the string representation of the duration to the time.Duration type. You can new do this by using the following code snippet:

{{ "1m32s" | toDuration | dateModify "+10s" }} // Output: 1m42s

For those looking for a solution, I recommend checking out the latest releases of the fork. This should help address the issue discussed in this thread.

Thank you!