java The default precision is in milliseconds , The generated timestamp is 13 position , and python The default is 10 Bit , The accuracy is seconds . that python How is it generated 13 A time stamp , And how time stamps are converted to dates ( year - month - Japan when - branch - second )
Python Realization 【 Time stamp 】 And 【 Date format 】 Application function summary table of mutual transformation :
time.time()
) Get a timestamp accurate to seconds ,10 position 1655179674int(round(time.time() * 1000)
) Get the exact millisecond timestamp ,13 position 1655179674912time.localtime
(k1) take 10 A time stamp k1 Convert to date format time.struct_time(tm_year=2022, tm_mon=6, tm_mday=11, tm_hour=18, tm_min=19, tm_sec=48, tm_wday=5, tm_yday=162, tm_isdst=0)time.strftime
(“%Y-%m-%d %H:%M:%S”, time.localtime
(k1)) take 10 A time stamp k1 To 【 year - month - Japan when - branch - second 】 Date format 2019-09-02 16:19:35time.localtime(k1/1000)
take 13 A time stamp k1 Convert to date format time.struct_time(tm_year=2022, tm_mon=6, tm_mday=11, tm_hour=18, tm_min=19, tm_sec=48, tm_wday=5, tm_yday=162, tm_isdst=0)time.strftime
(“%Y-%m-%d %H:%M:%S”, time.localtime
(k1/1000)) take 13 A time stamp k1 To 【 year - month - Japan when - branch - second 】 Date format 2019-09-02 16:19:35# -*- coding:utf-8 -*-
import time
# Get current date , To 10 Bit timestamp format
def get_second():
""" :return: Get a timestamp accurate to seconds ,10 position """
return int(time.time())
# Get current date , To 13 Bit timestamp format
def get_millisecond():
""" :return: Get the exact millisecond timestamp ,13 position """
millis = int(round(time.time() * 1000))
return millis
# Two 13 The timestamp of bits is subtracted , Return seconds
def get_delta(t1,t2):
""" :param t1: 13 A time stamp :param t2: 13 A time stamp :return: Subtract two timestamps , Return seconds """
res=int((t2 - t1)/1000)
return res
if __name__ == "__main__":
print(get_second()) # Get the current time , And turn to 10 Bit timestamp format
>>>
1655179674
print(time.time()) # Print the timestamp with full accuracy directly
>>>
1655179674.911647
time1=get_millisecond()
print(time1) # Get the current time , And turn to 13 Bit timestamp format
>>>
1655179674912
# Two 13 Bit timestamp for difference operation
k1=1567412375458
k2=1567412395853
now = int(round(time.time() * 1000))
print(now)
>>>
1655179674913
t1 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(k1/1000))
t2=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(k2/1000))
print(t1)
>>>
2019-09-02 16:19:35
print(t2)
>>>
2019-09-02 16:19:55
print(get_delta(k1,k2))
>>>
20
function 4 millisecond_to_time(millis):13 Bit timestamp converted to date format string
import time
# Enter the time in milliseconds , Time to transfer out of normal format
def timeStamp(timeNum):
timeStamp = float(timeNum/1000)
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)
time_st = 1654942788469 # Randomly assigned timestamp
timeStamp(time_st) # Call function
>>>
2022-06-11 18:19:48
Reference link :【1】 Online time conversion tool :https://tool.lu/timestamp