Skip to content

Instantly share code, notes, and snippets.

@dewe
Created April 12, 2024 06:04
Show Gist options
  • Save dewe/a3df56d854eaef71d7c584d7984ba0a1 to your computer and use it in GitHub Desktop.
Save dewe/a3df56d854eaef71d7c584d7984ba0a1 to your computer and use it in GitHub Desktop.
epoch formatting, shell function (mac os / BSD)
# epoch format, use with arg or pipe; no argument -> echo current epoch
function epoch() {
fmt="+%Y-%m-%dT%H:%M:%S%z"
if [ -t 0 ] && [ -z "$1" ]; then
# no input, echo current epoch
date "+%s"
elif [[ -z "$1" ]]; then
# no arg, use piped value
xargs -I {} sh -c "date -r {} $fmt"
else
date -r $1 $fmt
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment