求时间间隔

该代码实现了一个类Calculation,用于计算两个时间点之间的时间间隔,单位为分钟,并能转换为秒、小时、天、月和年。方法getMinutes_section根据输入的开始时间和结束时间,通过判断闰年和平年,精确计算出两者之间的分钟差值,并提供了相应的转换。
摘要由CSDN通过智能技术生成

说明

给定两个时间点,求时间间隔:分钟、小时、天、月、年
如: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}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值