说明
给定两个时间点,求时间间隔:分钟、小时、天、月、年
如:time1=[‘2025-02-25’, ‘10:20’],time2=[‘2021-10-12’, ‘18:05’]
class Calculation(object):
def __init__(self):
self.time_section = {"start_time": [], "end_time": [], "section_min": 0}
def __del__(self):
# 秒
self.time_section['section_second'] = self.time_section['section_min']*60
# 小时
self.time_section['section_hour'] = self.time_section['section_min']/60
# 天
self.time_section['section_day'] = self.time_section['section_min']/60/24
# 月
self.time_section['section_month'] = self.time_section['section_min']/60/24/30
# 年
self.time_section['section_year'] = self.time_section['section_min']/60/24/365
def getMinutes_section(self, start_time:list, end_time:list):
"""
求时间间隔,两个时间间隔多少分钟
eg:
getMinutes_section(start_time=['2021-09-24', '11:27'], end_time=['2021-09-24', '11:28'])
-> {'start_time': ['2021-01-24', '11:27'], 'end_time': ['2021-09-24', '11:28'], 'section_min': 348481}
:param start_time: 开始时间
:param end_time: 结束时间
:return: [int] 间隔时间分钟
"""
# 定义平年或瑞年按月份求和天数
day_sum_pingnian = lambda month: reduce(lambda x,y: x+y, [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][i] for i in range(month)])
day_sum_ruinian = lambda month: reduce(lambda x,y: x+y, [[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][i] for i in range(month)])
# 分割字符串,获取时间
s_year = int(start_time[0].split('-')[0])
s_month = int(start_time[0].split('-')[1])
s_day = int(start_time[0].split('-')[2])
s_hour = int(start_time[1].split(':')[0])
s_minute = int(start_time[1].split(':')[1])
e_year = int(end_time[0].split('-')[0])
e_month = int(end_time[0].split('-')[1])
e_day = int(end_time[0].split('-')[2])
e_hour = int(end_time[1].split(':')[0])
e_minute = int(end_time[1].split(':')[1])
if s_minute == e_minute:
self.time_section["section_min"] = 0
self.time_section["start_time"] = start_time
self.time_section["end_time"] = end_time
return self.time_section
if s_year < e_year:
start_time_large = 0
middle_time = 0
end_time_large = 0
for years in range(s_year, e_year+1):
if years == s_year:
# 判断是否是瑞年
if s_year % 4 == 0 and s_year % 100 != 0 or s_year % 400 == 0:
start_time_large = 366*24*60 - day_sum_ruinian(s_month)*24*60 - s_day*24*60 - s_hour*60 - s_minute
else:
start_time_large = 365*24*60 - day_sum_pingnian(s_month) * 24 * 60 - s_day * 24 * 60 - s_hour * 60 - s_minute
elif years == e_year:
# 判断是否是瑞年
if e_year % 4 == 0 and e_year % 100 != 0 or e_year % 400 == 0:
end_time_large = day_sum_ruinian(e_month)*24*60 + e_day*24*60 + e_hour*60 + e_minute
else:
end_time_large = day_sum_pingnian(e_month) * 24 * 60 + e_day * 24 * 60 + e_hour * 60 + e_minute
else:
# 判断是否是瑞年
if years % 4 == 0 and years % 100 != 0 or years % 400 == 0:
middle_time += 366*24*60
else:
middle_time += 365*24*60
self.time_section["section_min"] = end_time_large + middle_time + start_time_large
self.time_section["start_time"] = start_time
self.time_section["end_time"] = end_time
return self.time_section
elif s_year > e_year:
return self.getMinutes_section(start_time=end_time, end_time=start_time)
else:
if s_month < e_month:
# 判断是否是瑞年
if e_year % 4 == 0 and e_year % 100 != 0 or e_year % 400 == 0:
self.time_section["section_min"] = day_sum_ruinian(e_month)*24*60 + e_day*24*60 + e_hour*60 + e_minute - day_sum_ruinian(s_month)*24*60 - s_day*24*60 - s_hour*60 - s_minute
else:
self.time_section["section_min"] = day_sum_pingnian(e_month)*24*60 + e_day*24*60 + e_hour*60 + e_minute - day_sum_pingnian(s_month)*24*60 - s_day*24*60 - s_hour*60 - s_minute
self.time_section["start_time"] = start_time
self.time_section["end_time"] = end_time
return self.time_section
elif s_month > e_month:
return self.getMinutes_section(start_time=end_time, end_time=start_time)
else:
if s_day < e_day:
self.time_section["section_min"] = (e_day- s_day)*24*60 + e_hour*60 + e_minute - s_hour*60 - s_minute
self.time_section["start_time"] = start_time
self.time_section["end_time"] = end_time
return self.time_section
elif s_day > e_day:
return self.getMinutes_section(start_time=end_time, end_time=start_time)
else:
if s_hour < e_hour:
self.time_section["section_min"] = (e_hour - s_hour)*60 + e_minute - s_minute
self.time_section["start_time"] = start_time
self.time_section["end_time"] = end_time
return self.time_section
elif s_hour > e_hour:
return self.getMinutes_section(start_time=end_time, end_time=start_time)
else:
if s_minute < e_minute:
self.time_section["section_min"] = e_minute - s_minute
self.time_section["start_time"] = start_time
self.time_section["end_time"] = end_time
return self.time_section
else:
return self.getMinutes_section(start_time=end_time, end_time=start_time)
if __name__ == '__main__':
min = Calculation().getMinutes_section(start_time=['2025-02-25', '10:20'], end_time=['2021-10-12', '18:05'])
print(min)
运行结果:
{‘start_time’: [‘2021-10-12’, ‘18:05’], ‘end_time’: [‘2025-02-25’, ‘10:20’], ‘section_min’: 1769295, ‘section_second’: 106157700, ‘section_hour’: 29488.25, ‘section_day’: 1228.6770833333333, ‘section_month’: 40.95590277777777, ‘section_year’: 3.3662385844748854}