Last active
May 5, 2023 14:45
-
-
Save wader/5f8965a4db73db0d79496d6fcfbdca7a to your computer and use it in GitHub Desktop.
jq duration helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "01:02:03.45" -> 3723.45 | |
def from_duration: | |
( reduce (split(":") | reverse[]) as $p ( | |
{m: 1, n: 0}; | |
( .n = .n + ($p | tonumber) * .m | |
| .m *= 60 | |
) | |
) | |
| .n | |
); | |
# 5100 -> "1h 25mins" | |
def to_fancy_duration: | |
( [(./(60*60) | floor), .%(60*60)] as [$h,$r] | |
| $r | |
| [(./60 | floor), .%60] as [$m,$r] | |
| $r as $s | |
| [ if $h > 0 then "\($h)h" else empty end | |
, if $m > 0 then "\($m)mins" else empty end | |
, if $s > 0 then "\($s)secs" else empty end | |
] | |
| join(" ") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment