Skip to content

Instantly share code, notes, and snippets.

@Frick
Last active July 29, 2020 06:26
Show Gist options
  • Save Frick/9690629 to your computer and use it in GitHub Desktop.
Save Frick/9690629 to your computer and use it in GitHub Desktop.
This is a quick Linux shell script which, when bound to a keyboard shortcut, allows you to highlight a Unix timestamp (seconds, milliseconds, microseconds, hex) or a MongoDB ObjectID [double clicking to highlight/select works great here], mash your keyboard shortcut and see a popup of the date/time in your specified format. It also copies it to …
#!/bin/bash
DATE_FORMAT="%F %T %Z"
SEL=$(xclip -o | sed 's/[^0-9A-Fa-f]//g')
notify () {
# hack to allow more than one alert to be sent every 6 seconds
pkill -9 -f "notify-osd"
notify-send -i appointment-new "$1"
}
datetime () {
echo -n "$(date -d@"${1}" +"${DATE_FORMAT}")"
}
is_digits=$(echo "${SEL}" | grep -Eq "^[0-9]+$" >/dev/null 2>&1; echo -n $?)
is_hex=$(echo "${SEL}" | grep -Eq "^[0-9A-Fa-f]+$" >/dev/null 2>&1; echo -n $?)
if [[ $is_digits == 0 && ( ${#SEL} == 16 || ${#SEL} == 13 || ${#SEL} == 10 ) ]]; then
# us, ms or seconds since epoch
SEL="${SEL:0:10}"
datetime "${SEL}" | xclip -sel clip
notify "$(datetime "${SEL}")"
elif [[ $is_hex == 0 && ( ${#SEL} == 24 || ${#SEL} == 8 ) ]]; then
# seconds since epoch in hex or full MongoDB ObjectID
TS=$(echo "ibase=16; $(echo "${SEL:0:8}" | tr '[:lower:]' '[:upper:]')" | bc)
datetime "${TS}" | xclip -sel clip
notify "$(datetime "${TS}")"
else
# fail
pkill -9 -f "notify-osd"
notify-send -i face-angry "not a valid timestamp"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment