diff options
-rwxr-xr-x | .local/share/bin/timesum | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/.local/share/bin/timesum b/.local/share/bin/timesum new file mode 100755 index 0000000..448515d --- /dev/null +++ b/.local/share/bin/timesum @@ -0,0 +1,58 @@ +#!/bin/sh +grep -o '\<\([0-9]\+h\)\?\([0-9]\{1,2\}m\)\?\([0-9]\{1,2\}s\)\?\>' "$@" |\ + awk ' +BEGIN { + sum = 0 +} + +{ + temp = 0 + num = 0 + multiplier = 60 + + split($0, char, "") + for (i=1; i <= length($0); i++) { + if (char[i] == "h") { + temp += num * 60 * 60 + multiplier = 60 + num = 0 + continue + } + if (char[i] == "m") { + temp += num * 60 + multiplier = 1 + num = 0 + continue + } + if (char[i] == "s") { + temp += num + multiplier = 0 + num = 0 + continue + } + + num *= 10 + num += char[i] + } + if (num > 0) { + temp += num * multiplier + } + + # printf("%10s -> %d\n", $0, temp) + sum += temp +} + +END { + seconds = sum % 60 + sum = (sum - seconds) / 60 + minutes = sum % 60 + sum = (sum - minutes) / 60 + hours = sum + + if (hours > 0) printf("%dh", hours) + printf("%02dm", minutes) + if (seconds > 0) printf("%02ds", seconds) + printf("\n") +} +' + |