Created
November 27, 2019 18:17
-
-
Save dedeibel/c5d1df556614ddc20d16dc803ed9013c to your computer and use it in GitHub Desktop.
epoch - detect and print time value in various bases (ns us ms s) and format it as local and utc date
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
#!/bin/bash | |
#FORMAT="--iso-8601=ns" | |
FORMAT="+%Y-%m-%d %H:%M:%S" | |
TZ=`date ${UTC} +%z` | |
NANO=1000000000 | |
MICRO=1000000 | |
MILLI=1000 | |
MIN_SECONDS=1000000000 | |
if [[ $1 == "-l" ]]; then | |
shift | |
# could be an utc/local argument | |
fi | |
VAL=$1 | |
function dateFromSeconds { | |
S=$1 | |
SUB=$2 | |
FORMAT_SUFFIX= | |
if [[ -n $SUB ]]; then | |
FORMAT_SUFFIX=".${SUB}" | |
fi | |
date --utc "${FORMAT}${FORMAT_SUFFIX} %z" --date "@${S}" | |
echo | |
date "${FORMAT}${FORMAT_SUFFIX} %z" --date "@${S}" | |
} | |
function maskUpper { | |
VAL=$1 | |
PREC=$2 | |
echo $(($VAL / $PREC)) | |
} | |
function maskLower { | |
VAL=$1 | |
PREC=$2 | |
SECONDS_PART=$((($VAL / $PREC) * $PREC)) | |
echo $(($VAL - $SECONDS_PART)) | |
} | |
function printVariants { | |
VAL=$1 | |
PREC=$2 | |
IN_NANOS=`echo "scale=9; $VAL / ($PREC / $NANO)" | bc` | |
IN_MICROS=`echo "scale=9; $VAL / ($PREC / $MICRO)" | bc` | |
IN_MILLIS=`echo "scale=9; $VAL / ($PREC / $MILLI)" | bc` | |
IN_SECONDS=`echo "scale=9; $VAL / $PREC" | bc` | |
printf "%.0f ns\n%.3f us\n%.6f ms\n%.9f s" "$IN_NANOS" "$IN_MICROS" "$IN_MILLIS" "$IN_SECONDS" | |
#printf "%.0fns %.3fus %.6fms %.9fs" "1.0" "0.001" "0.000001" "0.000000001" | |
} | |
function fromNanos { | |
N=$1 | |
printVariants $N $NANO | |
echo -e "\n" | |
SECONDS=`maskUpper $N $NANO` | |
NANOS_ONLY=`maskLower $N $NANO` | |
NANOS_FORMATTED=`printf "%09dns" "$NANOS_ONLY"` | |
dateFromSeconds $SECONDS $NANOS_FORMATTED | |
echo -e "\nSubseconds:\n" | |
printVariants $NANOS_ONLY $NANO | |
echo | |
} | |
function fromMicros { | |
N=$1 | |
printVariants $N $MICRO | |
echo -e "\n" | |
SECONDS=`maskUpper $N $MICRO` | |
MICROS_ONLY=`maskLower $N $MICRO` | |
MICROS_FORMATTED=`printf "%06dus" "$MICROS_ONLY"` | |
dateFromSeconds $SECONDS $MICROS_FORMATTED | |
echo -e "\nSubseconds:\n" | |
printVariants $MICROS_ONLY $MICRO | |
echo | |
} | |
function fromMillis { | |
N=$1 | |
printVariants $N $MILLI | |
echo -e "\n" | |
SECONDS=`maskUpper $N $MILLI` | |
MILLIS_ONLY=`maskLower $N $MILLI` | |
MILLIS_FORMATTED=`printf "%03dms" "$MILLIS_ONLY"` | |
dateFromSeconds $SECONDS $MILLIS_FORMATTED | |
echo -e "\nSubseconds:\n" | |
printVariants $MILLIS_ONLY $MILLI | |
echo | |
} | |
function fromSeconds { | |
SECONDS=$1 | |
printVariants $SECONDS 1 | |
echo -e "\n" | |
dateFromSeconds $SECONDS | |
echo | |
} | |
if [[ $VAL -gt $(($MIN_SECONDS * $NANO)) ]]; then | |
fromNanos $VAL | |
elif [[ $VAL -gt $(($MIN_SECONDS * $MICRO)) ]]; then | |
fromMicros $VAL | |
elif [[ $VAL -gt $(($MIN_SECONDS * $MILLI)) ]]; then | |
fromMillis $VAL | |
elif [[ $VAL -gt $MIN_SECONDS ]]; then | |
fromSeconds $VAL | |
else | |
printVariants $VAL 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment