python常用方法----获取当前、分、时、日、月、年时间戳,当前时间前后,分、时、日、月、年开始、结束毫秒时间戳
作者:互联网
菜鸡的我,在写脚本的过程中发现要不断获取时间戳,且是不同时间段的,经过一番查阅完善有了下面的方法,大佬勿喷!
def getTime(time_type,LG):
'''
获取年,月,日,时,分前后的具体时间及开始和结束时间
:param time_type: 1:天;2:小时;3:分钟;4:月,5:年
:param LG: 整数值 0当前,1往后1分钟,小时,天,月,年,-1往前一分钟,小时,天,月,年。
:return: start_time:开始时间、end_time:结束时间、present_date:当前时间
'''
Date = datetime.datetime.now()
dates = None
start_time = None
end_time = None
present_time = None
if time_type <= 3:
if time_type == 1:
dates = datetime.timedelta(days=LG*-1)
elif time_type == 2:
dates = datetime.timedelta(hours=LG*-1)
elif time_type == 3:
dates = datetime.timedelta(minutes=LG*-1)
strtime = Date - dates
present = strtime.strftime('%Y-%m-%d %H:%M:%S')
present_date = time.strptime(present, "%Y-%m-%d %H:%M:%S")
present_time = int(time.mktime(present_date) * 1000)
if time_type == 1:
start = '{} 00:00:00'.format(present[0:-9])
start = time.strptime(start, "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start) * 1000)
end = '{} 23:59:59'.format(present[0:-9])
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end) * 1000)
elif time_type == 2:
start = '{}:00:00'.format(present[0:-6])
start = time.strptime(start, "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start) * 1000)
end = '{}:59:59'.format(present[0:-6])
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end) * 1000)
elif time_type == 3:
start = '{}:00'.format(present[0:-3])
start = time.strptime(start, "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start) * 1000)
end = '{}:59'.format(present[0:-3])
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end) * 1000)
else:
YTD = arrow.now()
if time_type == 4:
data_time = YTD.shift(months=+LG).format("YYYY-MM-DD")
data_times = YTD.shift(months=+LG).format("YYYY-MM-DD HH:mm:ss")
year = data_time[0:-5]
mouth = data_time[5:-3]
if int(data_time[5:-3]) in (1, 3, 5, 7, 8, 10, 12):
start = '{}{}-01 00:00:00'.format(year,mouth)
start = time.strptime(start, "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start) * 1000)
end = '{0}{1}-31 23:59:59'.format(year,mouth)
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end) * 1000)
present_date = time.strptime(data_times, "%Y-%m-%d %H:%M:%S")
present_time = int(time.mktime(present_date) * 1000)
elif int(data_time[5:-3]) == int('02'):
start = '{0}{1}-01 00:00:00'.format(year,mouth)
start = time.strptime(start, "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start) * 1000)
end = '{0}{1}-28 23:59:59'.format(year,mouth)
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end) * 1000)
present_date = time.strptime(data_times, "%Y-%m-%d %H:%M:%S")
present_time = int(time.mktime(present_date) * 1000)
else:
start = '{0}{1}-01 00:00:00'.format(year,mouth)
start = time.strptime(start, "%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start) * 1000)
end = '{0}{1}-30 23:59:59'.format(year,mouth)
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end) * 1000)
present_date = time.strptime(data_times, "%Y-%m-%d %H:%M:%S")
present_time = int(time.mktime(present_date) * 1000)
elif time_type == 5:
data_time = str(int(datetime.datetime.now().strftime("%Y"))+LG)+"-"+datetime.datetime.now().strftime("%m-%d")
data_times = str(int(datetime.datetime.now().strftime("%Y")) + LG) + "-" + datetime.datetime.now().strftime("%m-%d %H:%M:%S")
year = data_time[0:-6]
start = '{}-01-01 00:00:00'.format(year)
start = time.strptime(start,"%Y-%m-%d %H:%M:%S")
start_time = int(time.mktime(start)*1000)
end = '{}-12-31 23:59:59'.format(year)
end = time.strptime(end, "%Y-%m-%d %H:%M:%S")
end_time = int(time.mktime(end)*1000)
present_date = time.strptime(data_times, "%Y-%m-%d %H:%M:%S")
present_time = int(time.mktime(present_date) * 1000)
return start_time,end_time,present_time
标签:-%,end,python,----,start,int,时间,time,present 来源: https://www.cnblogs.com/gxda/p/16076710.html