工具通过时间戳获取事件段
通过时间戳获取时间段的,从获取到的结果中获取某个时间段内的结果的数量
1 #!/usr/bin/python 2 # coding=utf-8 3 4 # 按小时查询每小时对应事件的数量 5 import time 6 7 8 class GetTimeRange(object): 9 def according_hour(self, start_time, end_time, result): 10 li = [] 11 hour_list = [] 12 one_hour = 60 * 60 13 for hour in range(start_time, end_time + 1, one_hour): 14 hour_list.append(time.strftime("%Y/%m/%d %H:00:00", time.localtime(hour))) 15 hour_list = [item for item in hour_list] 16 hour_timestmap_list = [int(time.mktime(time.strptime(hour, "%Y/%m/%d %H:00:00"))) for hour in hour_list] 17 for index, hour_time in enumerate(hour_list): 18 data = {} 19 if result: 20 hour_time_result = [item[0] for item in result if 21 hour_timestmap_list[index] <= int(item[2]) < ( 22 hour_timestmap_list[index] + 60 * 60)] 23 data["hour_time_result"] = hour_time_result 24 data["date"] = hour_time 25 data["num"] = len(hour_time_result) 26 else: 27 data["hour_time_result"] = [] 28 data["date"] = hour_time 29 data["num"] = 0 30 li.append(data) 31 return li 32 33 # 按天查询每天对应的事件数量 34 def according_day(self, start_time, end_time, result): 35 li = [] 36 one_day = 60 * 60 * 24 37 day_list = [] 38 for day in range(start_time, end_time + 1, one_day): 39 day_list.append(time.strftime("%Y/%m/%d", time.localtime(day))) 40 day_list = [item for item in day_list] 41 day_timestmap_list = [int(time.mktime(time.strptime(day, "%Y/%m/%d"))) for day in day_list] 42 for index, day_time in enumerate(day_list): 43 data = {} 44 if result: 45 day_time_result = [item[0] for item in result if day_timestmap_list[index] <= int(item[2]) < ( 46 day_timestmap_list[index] + 24 * 60 * 60)] 47 data["day_time_result"] = day_time_result 48 data["date"] = day_time 49 data["num"] = len(day_time_result) 50 else: 51 data["date"] = day_time 52 data["num"] = 0 53 li.append(data) 54 return li 55 56 57 if __name__ == '__main__': 58 timeer = GetTimeRange() 59 print(timeer.according_hour(1545275344, 1545361744, [])) 60 """ 61 1545275344>>>2018-12-20 11:09:04 62 1545361744>>>2018-12-21 11:09:04 63 a = [ 64 {'date': '2018/12/20 11:00:00', 'hour_time_result': [], 'num': 0}, 65 {'date': '2018/12/20 12:00:00', 'hour_time_result': [], 'num': 0}, 66 {'date': '2018/12/20 13:00:00', 'hour_time_result': [], 'num': 0}, 67 {'date': '2018/12/20 14:00:00', 'hour_time_result': [], 'num': 0}, 68 {'date': '2018/12/20 15:00:00', 'hour_time_result': [], 'num': 0}, 69 {'date': '2018/12/20 16:00:00', 'hour_time_result': [], 'num': 0}, 70 {'date': '2018/12/20 17:00:00', 'hour_time_result': [], 'num': 0}, 71 {'date': '2018/12/20 18:00:00', 'hour_time_result': [], 'num': 0}, 72 {'date': '2018/12/20 19:00:00', 'hour_time_result': [], 'num': 0}, 73 {'date': '2018/12/20 20:00:00', 'hour_time_result': [], 'num': 0}, 74 {'date': '2018/12/20 21:00:00', 'hour_time_result': [], 'num': 0}, 75 {'date': '2018/12/20 22:00:00', 'hour_time_result': [], 'num': 0}, 76 {'date': '2018/12/20 23:00:00', 'hour_time_result': [], 'num': 0}, 77 {'date': '2018/12/21 00:00:00', 'hour_time_result': [], 'num': 0}, 78 {'date': '2018/12/21 01:00:00', 'hour_time_result': [], 'num': 0}, 79 {'date': '2018/12/21 02:00:00', 'hour_time_result': [], 'num': 0}, 80 {'date': '2018/12/21 03:00:00', 'hour_time_result': [], 'num': 0}, 81 {'date': '2018/12/21 04:00:00', 'hour_time_result': [], 'num': 0}, 82 {'date': '2018/12/21 05:00:00', 'hour_time_result': [], 'num': 0}, 83 {'date': '2018/12/21 06:00:00', 'hour_time_result': [], 'num': 0}, 84 {'date': '2018/12/21 07:00:00', 'hour_time_result': [], 'num': 0}, 85 {'date': '2018/12/21 08:00:00', 'hour_time_result': [], 'num': 0}, 86 {'date': '2018/12/21 09:00:00', 'hour_time_result': [], 'num': 0}, 87 {'date': '2018/12/21 10:00:00', 'hour_time_result': [], 'num': 0}, 88 {'date': '2018/12/21 11:00:00', 'hour_time_result': [], 'num': 0} 89 ] 90 91 """