折腾:
期间,需要去获取带毫秒的13位数的时间戳,比如:#(2)http://xxx.cn/video/54333
# interface: Vod_Api_GetPlayInfo
# 1: 1
# file_id: 7447398155847467190
# app_id: 1252879503
# refer: xxx.com.cn
# _: 1531464292921
# callback: qcvideo_1531464290730_callback1
中的:
1531464292921
而之前自己的python库:
util/crifanLib/crifanDatetime.pyfrom datetime import datetime,timedelta
import time
def getCurTimestamp():
"""
get current time's timestamp
eg: 1351670162
"""
return datetimeToTimestamp(datetime.now())
def datetimeToTimestamp(datetimeVal) :
"""
convert datetime value to timestamp
eg:
"2006-06-01 00:00:00" -> 1149091200
:param datetimeVal:
:return:
"""
return int(time.mktime(datetimeVal.timetuple()))
只能获取不带毫秒的,10位数的时间戳
python timestamp with milliseconds
去查查timetuple
python timetuple
没有找到更加清楚的解释
不过自己从
“In [57]: datetime.datetime.now().timetuple()
Out[57]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=9, tm_min=58, tm_sec=12, tm_wday=4, tm_yday=227, tm_isdst=-1)
In [58]: time.mktime(datetime.datetime.now().timetuple())
Out[58]: 1408067904.0″
以及timetuple名字看出来是:
把time以tuple元组的方式输出了年月日时分秒,但是没有millisecond毫秒
不过自己调试期间发现,即使此处用Python 2,datetime也直接有microsecond微秒:
所以可以直接拿来使用了。
后来看到:
“datetime.timetuple()
Return atime.struct_timesuch as returned bytime.localtime().d.timetuple()is equivalent totime.struct_time((d.year, d.month,d.day, d.hour, d.minute, d.second, d.weekday(), yday, dst)), whereyday = d.toordinal() – date(d.year, 1, 1).toordinal() + 1is the day number within the current year starting with1for January 1st. Thetm_isdstflag of the result is set according to thedst()method:tzinfoisNoneordst()returnsNone,tm_isdstis set to-1; else ifdst()returns a non-zero value,tm_isdstis set to1; elsetm_isdstis set to0.“
果然是没有毫秒(或微秒)的
“datetime.microsecond
Inrange(1000000).“
嗯 python 2和python 3都有这个属性。
【总结】
可以放心去利用datetime.microsecond去实现带毫秒的时间戳了:from datetime import datetime,timedelta
import time
def getCurTimestamp(withMilliseconds=False):
"""
get current time's timestamp
(default)not milliseconds -> 10 digits: 1351670162
with milliseconds -> 13 digits: 1531464292921
"""
curDatetime = datetime.now()
return datetimeToTimestamp(curDatetime, withMilliseconds)
def datetimeToTimestamp(datetimeVal, withMilliseconds=False) :
"""
convert datetime value to timestamp
eg:
"2006-06-01 00:00:00.123" -> 1149091200
if with milliseconds -> 1149091200123
:param datetimeVal:
:return:
"""
timetupleValue = datetimeVal.timetuple()
timestampFloat = time.mktime(timetupleValue) # 1531468736.0 -> 10 digits
timestamp10DigitInt = int(timestampFloat) # 1531468736
timestampInt = timestamp10DigitInt
if withMilliseconds:
microsecondInt = datetimeVal.microsecond # 817762
microsecondFloat = float(microsecondInt)/float(1000000) # 0.817762
timestampFloat = timestampFloat + microsecondFloat # 1531468736.817762
timestampFloat = timestampFloat * 1000 # 1531468736817.7621 -> 13 digits
timestamp13DigitInt = int(timestampFloat) # 1531468736817
timestampInt = timestamp13DigitInt
return timestampInt
def testTimestamp():
# test timestamp with milliseconds
timestampNoMilliSec = getCurTimestamp()
print("timestampNoMilliSec=%s" % timestampNoMilliSec) # 1531468833
timestampWithMilliSec = getCurTimestamp(withMilliseconds=True)
print("timestampWithMilliSec=%s" % timestampWithMilliSec) # 1531468833344
最新代码详见: