调度任务中常常要用到精确的时间,如果获取的13位时间戳不够准确的话,会造成数据的重复或者缺失
通常的不准确的13位时间戳的获取方式(秒*1000)
import time
millis = int(round(() * 1000))
print millis
准确的13位时间戳获取(微妙/1000)
# -*- coding: utf-8 -*-
import datetime
import time
"""
获取精确毫秒数时间戳、
"""
def get_seconds():
datetime_object = ()
now_timetuple = datetime_object.timetuple()
now_second = (now_timetuple)
mow_millisecond = long(now_second*1000 + datetime_object.microsecond/1000)
print "timetuple-- "+ str(now_timetuple)
print "datimeobject-- " + str(datetime_object)
print "second-- " + str(now_second)
print "millisecond-- " + str(mow_millisecond)
if __name__ == "__main__":
get_seconds()
结果
timetuple-- time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=11, tm_min=55, tm_sec=6, tm_wday=0, tm_yday=337, tm_isdst=-1)
datimeobject-- 2018-12-03 11:55:06.254679
second-- 1543809306.0
millisecond-- 1543809306254
以下是转载的 字符串与时间戳转换的方式 来自于:/2016/03/19/python%20%E6%97%B6%E9%97%B4%E6%88%B3%E5%A4%84%E7%90%86/
# -*- coding: utf-8 -*-
import time
from datetime import datetime
def timestamp_to_strtime(timestamp):
"""将 13 位整数的毫秒时间戳转化成本地普通时间 (字符串格式)
:param timestamp: 13 位整数的毫秒时间戳 (1456402864242)
:return: 返回字符串格式 {str}'2016-02-25 20:21:04.242000'
"""
local_str_time = (timestamp / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
return local_str_time
def timestamp_to_datetime(timestamp):
"""将 13 位整数的毫秒时间戳转化成本地普通时间 (datetime 格式)
:param timestamp: 13 位整数的毫秒时间戳 (1456402864242)
:return: 返回 datetime 格式 {datetime}2016-02-25 20:21:04.242000
"""
local_dt_time = (timestamp / 1000.0)
return local_dt_time
def datetime_to_strtime(datetime_obj):
"""将 datetime 格式的时间 (含毫秒) 转为字符串格式
:param datetime_obj: {datetime}2016-02-25 20:21:04.242000
:return: {str}'2016-02-25 20:21:04.242'
"""
local_str_time = datetime_obj.strftime("%Y-%m-%d %H:%M:%S.%f")
return local_str_time
def datetime_to_timestamp(datetime_obj):
"""将本地(local) datetime 格式的时间 (含毫秒) 转为毫秒时间戳
:param datetime_obj: {datetime}2016-02-25 20:21:04.242000
:return: 13 位的毫秒时间戳 1456402864242
"""
local_timestamp = long((datetime_obj.timetuple()) * 1000.0 + datetime_obj.microsecond / 1000.0)
return local_timestamp
def strtime_to_datetime(timestr):
"""将字符串格式的时间 (含毫秒) 转为 datetiem 格式
:param timestr: {str}'2016-02-25 20:21:04.242'
:return: {datetime}2016-02-25 20:21:04.242000
"""
local_datetime = (timestr, "%Y-%m-%d %H:%M:%S.%f")
return local_datetime
def strtime_to_timestamp(local_timestr):
"""将本地时间 (字符串格式,含毫秒) 转为 13 位整数的毫秒时间戳
:param local_timestr: {str}'2016-02-25 20:21:04.242'
:return: 1456402864242
"""
local_datetime = strtime_to_datetime(local_timestr)
timestamp = datetime_to_timestamp(local_datetime)
return timestamp
def current_datetime():
"""返回本地当前时间, 包含datetime 格式, 字符串格式, 时间戳格式
:return: (datetime 格式, 字符串格式, 时间戳格式)
"""
# 当前时间:datetime 格式
local_datetime_now = ()
# 当前时间:字符串格式
local_strtime_now = datetime_to_strtime(local_datetime_now)
# 当前时间:时间戳格式 13位整数
local_timestamp_now = datetime_to_timestamp(local_datetime_now)
return local_datetime_now, local_strtime_now, local_timestamp_now
if __name__ == '__main__':
time_str = '2016-02-25 20:21:04.242'
timestamp1 = strtime_to_timestamp(time_str)
datetime1 = strtime_to_datetime(time_str)
time_str2 = datetime_to_strtime(datetime1)
timestamp2 = datetime_to_timestamp(datetime1)
datetime3 = timestamp_to_datetime(timestamp2)
time_str3 = timestamp_to_strtime(timestamp2)
current_time = current_datetime()
print 'timestamp1: ', timestamp1
print 'datetime1: ', datetime1
print 'time_str2: ', time_str2
print 'timestamp2: ', timestamp2
print 'datetime3: ', datetime3
print 'time_str3: ', time_str3
print 'current_time: ', current_time
使用 datetime模块来计算时间间隔
python中通过datetime模块可以很方便的计算两个时间的差,datetime的时间差单位可以是天、小时、秒,甚至是微秒,下面我们就来详细看下datetime的强大功能:
from datetime import datetime
a=()
b=()
>>>a
>>>(2015, 4, 7, 4, 30, 3, 628556)
>>>b
>>>(2015, 4, 7, 4, 34, 41, 907292)
>>>str(a) #字符串的转换,用户储存到文本或者数据库
'2015-04-07 04:30:03.628556'
>>>(str(a),"%Y-%m-%d %H:%M:%S.%f")
(2015, 4, 7, 4, 30, 3, 628556)
>>> (b-a)
Out: (0, 278, 278736)
>>>(b-a).seconds #时间差的计算,单位为秒
278
Q:如何方便的计算两个时间的差,如两个时间相差几天,几小时等
A:使用datetime模块可以很方便的解决这个问题,举例如下:
>>> import datetime>>> d1 = (2005, 2, 16)>>> d2 = (2004, 12, 31)>>> (d1 - d2).days47
上例演示了计算两个日期相差天数的计算。
>>>import datetime
>>>starttime = ()
#long running
>>>endtime = ()
>>>print (endtime - starttime).seconds
上例演示了计算运行时间的例子,以秒进行显示。
>>> d1 = () >>> d3 = d1 + (hours=10) >>> ()
上例演示了计算当前时间向后10小时的时间。
其本上常用的类有:datetime和timedelta两个。它们之间可以相互加减。每个类都有一些方法和属性可以查看具体的值,
如datetime可以查看:天数(day),小时数(hour),星期几(weekday())等;timedelta可以查看:天数(days),秒数(seconds)等。