#!/bin/bash set -eo pipefail # safest to die on any error or pipe fail temp_file=$(mktemp) # makes random temp file user has rw on, always safer to use mktemp for random temp name (unpredictable) and to avoid clobbering if [ -z "$1" ]; then # if no arg is present, select create new, current umicros code unix_microseconds=$(date +'%s%N') # microseconds since the unix epoch, UTC hex_encoding="$(printf '%x' $unix_microseconds)" # printf to convert da big int to hex echo -n "$hex_encoding" | xxd -p -r > "$temp_file" # decode hex to raw bytes with xxd, load the bytes into the temp file, must use temp file or nullbyte fuckery happenz ERROR TOWNN~ oh echo -n is to echo wothoot da appended newline base64_encoding="$(base64 ${temp_file} | tr -d '\n')"; # base64 encode the bytes from the temp file, then remove any newlines final_umicros_code="$(echo -n $base64_encoding | tr -d '\n' | tr '\/\+\=' '\-\.\_')" # now remove any dangling newlines and replace "/", "+", and "=" with "-", ".", and "_", respectively. just to make the code better suited to filenames echo -n "${final_umicros_code}" # output the umicros code (no trailing newline) # exit 0 # exit without error else # is an argumnenbt is specified, select to decode that argument as a umicros code umicros_code_supplied="$1" # get arg decoded_base64=$(echo -n "$umicros_code_supplied" | tr '\-\.\_' '\/\+\=' | base64 -d | xxd -p) # replace supplied umicros code with the original base64 chars, decode the base64, then convert to hex with xxd decoded_umicros_code_unix_microseconds=$(printf "%u" "0x${decoded_base64}") # prepend 0x and decode with printf (%u because its large and not likely to be signed) decoded_unix_seconds="${decoded_umicros_code_unix_microseconds:0:-9}" # get the first nine chars last_nine="${decoded_umicros_code_unix_microseconds: -9}" # get the last nine chars date -d "@${decoded_unix_seconds}.${last_nine}" +"%Y-%m-%d %H:%M:%S.%N %Z" # date command to format the umicros code, -u to use UTC time, -d is to set the timestamp to convert, unix microseconds converted to unix seconds by being broken into first six and last six digits, decimal between them. then just the datetime format, including the TZ (%Z/UTC) fi rm -f "$temp_file" # clean up temp file