Skip to content

Instantly share code, notes, and snippets.

@jenrik
Last active March 7, 2023 19:08
Show Gist options
  • Save jenrik/b8521bfe12129fd7982ff3562aa8e162 to your computer and use it in GitHub Desktop.
Save jenrik/b8521bfe12129fd7982ff3562aa8e162 to your computer and use it in GitHub Desktop.
Multifunction timestamp tool
function utime()
{
if [ $# = 1 ] && ([ "${1:0:1}" = "+" ] || [ "${1:0:1}" = "-" ]) && [ "$1" != "--help" ]
then
# Do date math
date -d "$1" "+%s"
elif [ $# = 1 ] && (echo "$1" | grep -P "^[[:digit:]]+$" > /dev/null)
then
# Convert timestamp to human-readable date
date -d "@$1" || help
elif [ $# = 0 ]
then
# Print timestamp
date "+%s"
else
echo "$0 usage:"
echo ""
echo "--help: this message"
echo "no arguments: current time as timestamp"
echo "+10days: now plus 10 days as timestamp"
echo "1000000: convert timestamp to human-readable date"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment