Various nanosecond results between GNU date and Python time module

I want to create a hex of the current number of nanoseconds since unix. (No need to be right, just accurate)

Here you can get the current nanoseconds in bash:

ksoviero@ksoviero-Latitude-E7440:~$ date +%s%N
1401993044859711108

Now, to turn it into hexadecimal, we use printf:

ksoviero@ksoviero-Latitude-E7440:~$ printf '%x\n' $(date +%s%N)
1374e157ee379028

See how easy it was?

Now try to do the same with Python. First, we get the current nanoseconds:

>>> from time import time
>>> print int(time() * 10**9)
1401993206893091840

Now we convert it to hex:

>>> print format(int(time() * 10**9), 'x')
1374e172f90a6400

I what!? Where did these two zeros come from at the end? They are always there, and they should not be ...

Ok, hold on, maybe Python hex functions can't handle large numbers. What happens if we just copy and paste nanoseconds?

>>> print format(1401993044859711108, 'x')
1374e13f086e6e84

Wait, it worked !?

- , ? , , ?

+4
3

time.time() float, .

+4

, time.clock:

import time

>>> format(int(time.clock()* 10**9), 'x')
'baaa3a9e9e9'
>>> format(int(time.clock()* 10**9), 'x')
'baba3cb2a5e'
>>> format(int(time.time()* 10**9), 'x')
'1374e44edaf2e400'
>>> format(int(time.time()* 10**9), 'x')
'1374e4500aeeb700'

time.clock ( ):

Unix . , , " ", C , , Python .

Windows , , Win32 QueryPerformanceCounter(). , .

, :

, . . Unix "" 50 100 .

, () sleep() , Unix: , time() ( Unix gettimeofday(), ), sleep() (Unix select() , ).

+1

. , , 0x. :

0x1374e17396ef0800
  1374e172f90a6400

, .

:

>>> now = time()
>>> print format(int(now * 10**9), 'x')
1374e1ffdd1a5300
>>> print hex(int(now * 10**9))
0x1374e1ffdd1a5300

You see that the outputs are the same. You need to save the output timebetween these two print statements, because otherwise, when you call it twice, it returns two different values.

0
source

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


All Articles