Created
July 18, 2022 17:57
-
-
Save AlexAtkinson/0b809e56fef7af00a0e27ad86b3fd6dd to your computer and use it in GitHub Desktop.
BASH: Simple TimeStamp helper
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
#!/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