Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Created July 18, 2022 17:57
Show Gist options
  • Save AlexAtkinson/0b809e56fef7af00a0e27ad86b3fd6dd to your computer and use it in GitHub Desktop.
Save AlexAtkinson/0b809e56fef7af00a0e27ad86b3fd6dd to your computer and use it in GitHub Desktop.
BASH: Simple TimeStamp helper
#!/usr/bin/env sh
# ts-helper.sh
# ------------------------------------------------------------------------------
# This is a demo of how to get aligned timestamps across any OS with python in
# the $PATH.
# ------------------------------------------------------------------------------
function ts() {
local s_int=$(date +'%s')
local s_len=${#s_int}
local sns=$(python -c 'import time; print(int(time.time_ns()))')
local s=${sns:0:$s_len}
local ns=${sns:$s_len}
printf "%s\n" "This helper ensures timestamp format alignment" \
"across Unix, Linux, MacOS, and BSD operating systems." \
"Note: It should function anywhere python is in the \$PATH " \
" and 'date' supports the '-u' and '-d' args." \
"" \
"s & ns: $sns" \
"s: $s" \
"ns: $ns" \
"ts-s: $(date -ud @$s +'%FT%TZ')" \
"ts-ns: $(date -ud @$s +%FT%T.${ns}Z)" \
"ts-ns-3: $(date -ud @$s +%FT%T.${ns:0:3}Z)"
}
ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment