When developing web programs in Python, the relevant interfaces of third parties need to be called, and the request needs to be signed when called. A Unix timestamp is required. In Python, a lot of methods that are introduced on the web, get a 10-bit timestamp. The default in Java is 13 milliseconds.

Here’s how Python gets the timestamp:

1, 10 Time stamp acquisition method:

>>> import time
>>> t = time.time()
>>> print t
1436428326.76
>>> print int(t)
1436428326
Copy the code

The cast simply removes the decimal place.

2. Method of obtaining 13-bit timestamp:

(1) By default, Python timestamps float output in seconds

>>> import time
>>> time.time()
1436428275.207596
Copy the code

Obtain the 13-bit timestamp by converting seconds to milliseconds:

import time
millis = int(round(time.time() * 1000))
print millis
Copy the code

Round () is rounding.

(2)

import time

current_milli_time = lambda: int(round(time.time() * 1000))
current_milli_time()
1378761833768
Copy the code

13 bit timestamp converted to time:

>>> import time
>>> now = int(round(time.time()*1000))
>>> now02 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now/1000))
>>> now02
'the 2017-11-07 16:47:14'
Copy the code