Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active October 16, 2022 18:19
Show Gist options
  • Save ernstki/c19a7d0885e3fa132f5ae07c0dbdfa26 to your computer and use it in GitHub Desktop.
Save ernstki/c19a7d0885e3fa132f5ae07c0dbdfa26 to your computer and use it in GitHub Desktop.
Awk function to print seconds elapsed in a human-friendly format
##
## Awk function to print seconds elapsed in a human-friendly format
##
## via: https://stackoverflow.com/q/12199631#comment71504103_28451379
##
function humantime(secs, fmt) {
fmt = "%2dh %2dm %2ds"
if (secs/86400 > 1) {
fmt = "%2dd " fmt # prepend days to the output
return sprintf(fmt, secs/86400, secs%86400/3600, secs%3600/60, secs%60)
} else {
return sprintf(fmt, secs%86400/3600, secs%3600/60, secs%60)
}
}
# vim: ft=awk
@ernstki
Copy link
Author

ernstki commented Oct 16, 2022

Credit the original author (https://stackoverflow.com/users/2195972/blue) if you wish, but it's just math here, so I would consider this code snippet to be in the public domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment