Get hexadecimal timestamp from bash script

I would like to convert the current date and time to a hex timestamp, for example:

Tuesday February 2 10:27:46 GMT 2010 converted to 0x6d054a874449e

I would like to do this from a bash script, any idea how I can do this?

Thanks J

+3
source share
3 answers
printf '0x% x' $ (date +% s)
+6
source

Without knowing the units or era for the timestamp of the hexadecimal text, it’s hard to say for sure (and I was a little confused by your example “February 2”, which is not even close to the current date!).

date +%s time_t, Unix ( 1 1970 ).

printf "0x%x" some_number .

/, . bash $(( expression )):

$ time_t=$(date +%s)
$ echo $(($time_t * 1000))
1284505668000

(, "2 ..." ), , , GNU date, -d +%s :

$ date -d 'Tue Feb 2 10:27:46 GMT 2010' +%s 
1265106466

:

$ time_t=$(date -d 'Tue Feb 2 10:27:46 GMT 2010' +%s)
$ time_t_ms=$(($time_t * 1000))
$ hexstamp=$(printf "0x%x" $time_t_ms)
$ echo $hexstamp
0x1268e38b4d0
+3

Seconds since unix, in hexadecimal format:

 echo "$(date +%s)"|xargs printf "0x%x"
 0x59a8de5b

Milliseconds from the era:

 echo "$(date +%s%N)/1000000"|bc|xargs printf "0x%x"
 0x15e3ba702bb

microseconds:

echo "$(date +%s%N)/1000"|bc|xargs printf "0x%x"
0x55818f6eea775

nanoseconds:

echo "$(date +%s%N)"|xargs printf "0x%x"
0x14e0219022e3745c
0
source

Source: https://habr.com/ru/post/1764766/


All Articles