How to generate timestamp in python

爱喝马黛茶的安东尼
Release: 2019-06-21 10:03:05
Original
22650 people have browsed it

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 >>>
Copy after login

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 >>>
Copy after login

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

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

round() is rounded.

(2)

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

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'
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!