标签:off method now() handler strftime 定时 offset bsp next
直接上代码
class DateHandler: @staticmethod def get_date(cur_date, offset_year): # 时间算法,往后偏移的年份; cur_day = cur_date.day cur_month = cur_date.month cur_year = cur_date.year if cur_day > 1: # 天数大于一 return cur_date.replace(year=cur_year + offset_year, day=cur_day - 1) else: if cur_month > 1: # 天数等于一,但是月份大于一 if cur_month - 1 in (1, 3, 5, 7, 8, 10, 12): return cur_date.replace(year=cur_year + offset_year, month=cur_month - 1, day=31) elif cur_month - 1 in (4, 6, 9, 11): return cur_date.replace(year=cur_year + offset_year, month=cur_month - 1, day=30) elif cur_month - 1 == 2: next_year = cur_year + offset_year if (next_year % 4 == 0 and next_year % 100 != 0) or (next_year % 400 == 0): # 普通闰年和世纪闰年 return cur_date.replace(year=cur_year + offset_year, month=cur_month - 1, day=29) else: return cur_date.replace(year=cur_year + offset_year, month=cur_month - 1, day=28) else: # 天数等于1、月份等于1;每年的12月31日缴纳 return cur_date.replace(year=cur_year + offset_year - 1,month=12, day=31) @staticmethod def date_str_to_datetime(time): # ‘2018-01-01.862458‘ 时间精确 if type(time) == str: return datetime.datetime.strptime(time,‘%Y-%m-%d.%f‘) elif type(time) == datetime: return datetime.datetime.now().strftime(‘%Y-%m-%d.%f‘) print(DateHandler.get_date(DateHandler.date_str_to_datetime(‘2018-01-01.862458‘),3))
根据给定时间及偏移的年份求偏移后时间的前一天(支持偏移量为正和负)
标签:off method now() handler strftime 定时 offset bsp next
原文地址:https://www.cnblogs.com/aadmina/p/10314912.html