python 判断一个时间是另一个时间往前推的第几个季度

需求

给两个字符串类型的时间,reference_date 是参考时间,计算 current_date 是在 reference_date 之前的第几个季度,reference_date 所在的当前季度为第0季度。
   

例如 reference_date 取值为 2024-07-14 00:00:00, 则 current_date 取值在 2024-04-01 00:00:00 到 2024-06-30 23:59:59 之间就是第1季度,返回1,如果取值在 2024-01-01 00:00:00 到 2024-03-31 23:59:59 之间就是第2季度,返回2,如果取值在 2023-10-01 00:00:00 到 2023-12-31 23:59:59 之间就是第3季度,返回3,以此类推

基本思路

主要是为了计算 current_date 是在 reference_date 前的季度数,这跟我们平时理解的季度是相反的。

1、先计算出当前时间 current_date 和参考时间 reference_date 分别属于当年的第几季度

2、再计算出当前时间和参考时间之间的年份差

3、如果在同一个年份,那么参考时间所在的季度数减去1(减去当前季度)就是最终结果

4、如果跨年了,那么在3步骤的基础上,再加上跨年所在的季度数,如果跨了完整的1年,就是加4,不完整的1年,就要加上当前时间所在季度的倒置(即第1季度加4,第2季度加3,第3季度加2,第4季度加1)。设置 quarter_interval = [0, 4, 3, 2, 1],这一段跨年的计算公式就是:(years_diff - 1) * 4 + quarter_interval[current_quarter]

实现

def get_quarter_num(reference_data_str, current_data_str):
    """
    判断该条数据属于过去哪个季度
    :param reference_data_str: 参考时间
    :param current_data_str: 当前时间
    :return:
    """
    quarter_interval = [0, 4, 3, 2, 1]
    # 将字符串转换为datetime对象
    current_data = datetime.strptime(current_data_str, '%Y-%m-%d %H:%M:%S')
    reference_data = datetime.strptime(reference_data_str, '%Y-%m-%d %H:%M:%S')

    # 确保reference_data在current_data之后
    if current_data >= reference_data:
        return -1

    # 当前日期和参考日期的年份和季度
    current_year = current_data.year
    current_quarter = (current_data.month - 1) // 3 + 1
    reference_year = reference_data.year
    reference_quarter = (reference_data.month - 1) // 3 + 1

    # 计算年份差和季度差
    years_diff = reference_year - current_year

    # 参考季度数-1 + (相差年数-1)*4 + quarter_interval[当前季度数]
    return reference_quarter - 1 + (years_diff - 1) * 4 + quarter_interval[current_quarter]


if __name__ == '__main__':
    reference_loan_data = "2024-12-01 00:00:00"
    current_loan_data = ["2024-03-31 23:59:59"
        , "2024-01-01 00:00:00"
        , "2024-02-29 23:39:02"
        , "2024-04-01 23:39:02"
        , "2024-05-25 23:39:02"
        , "2024-06-29 23:39:02"
        , "2024-07-07 23:39:02"
        , "2024-08-01 23:39:02"
        , "2024-10-29 23:39:02"
        , "2024-11-29 23:39:02"
        , "2023-12-31 23:59:59"
        , "2023-10-01 00:00:00"
        , "2023-09-30 23:59:59"
        , "2023-07-01 00:00:00"
        , "2023-06-30 23:59:59"
        , "2023-04-01 00:00:00"
        , "2023-02-28 23:59:59"
        , "2023-01-01 00:00:00"
        , "2022-12-31 23:59:59"
        , "2022-10-01 00:00:00"
        , "2022-09-30 23:59:59"
        , "2022-07-01 00:00:00"
        , "2022-06-30 23:59:59"
        , "2022-04-01 00:00:00"
        , "2022-02-28 23:59:59"
        , "2022-01-01 00:00:00"
        , "2021-12-31 23:59:59"
                         ]
    for current in current_loan_data:
        print("{}, {}, {}".format(reference_loan_data, current,
                                get_quarter_num(reference_loan_data, current)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值