Last active
March 7, 2023 19:08
-
-
Save jenrik/b8521bfe12129fd7982ff3562aa8e162 to your computer and use it in GitHub Desktop.
Multifunction timestamp tool
This file contains 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
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