Home  >  Article  >  Backend Development  >  How to generate timestamp in python

How to generate timestamp in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-21 10:03:0522748browse

When developing web programs in Python, you need to call third-party related interfaces. When calling, you need to sign the request. Need to use unix timestamp.

In python, many methods introduced on the Internet get a timestamp of 10 digits. The default in java is 13 bits (milliseconds, milliseconds).

How to generate timestamp in python

The following introduces the method of obtaining the timestamp in python:

1. 10 Timestamp obtaining method:

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

The forced conversion is directly removed Decimal places.

Related recommendations: "Python Video Tutorial"

2. How to obtain the 13-digit timestamp:

(1) Python time by default The stamp is a float output in seconds.

>>> 
>>> import time
>>> time.time()
1436428275.207596
>>>

Obtain a 13-digit timestamp by converting seconds to milliseconds:

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

round() is rounded.

(2)

import time
current_milli_time = lambda: int(round(time.time() * 1000))
Then:
>>> current_milli_time()
1378761833768

13-digit timestamp converted into time:

>>> import time
>>> now = int(round(time.time()*1000))
>>> now02 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now/1000))
>>> now02
'2019-06-21 09:53:17'

The above is the detailed content of How to generate timestamp in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn