With date
it is possible to generate formatted time strings like
date +"%Y-%m-%d-%H:%M:%S.%N"
With date
it is also possible to create unix timestamps in nano seconds. Using
NANO_TIMESTAMP=$(date +%s%N)
Is it possible to use date
to read in a nano second timestamp to create a formatted date string?
How can I pass a nano second timestamp to date
?
I tried:
date -d$NANO_TIMESTAMP +%H:%M:%S.%N
date: invalid date ‘1550736813798767689’
date -d@$NANO_TIMESTAMP +"%Y-%m-%d-%H:%M:%S.%N"
date: time 1550736813798767689 is out of range
Like this but without math.
NANO_TIMESTAMP=$(date +%s%N)
secs=$(printf "%1d\n" ${NANO_TIMESTAMP: 0 : -9})
nanos=${NANO_TIMESTAMP: -9 : 9 }
printf '\r%s' $(TZ=UTC date -d@$secs.$nanos +"%Y-%m-%d-%H:%M:%S.%N")
With printf "%1d"
I want to make sure, that there is at least one zero in the secs
variable.