You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
report_app/app/tools/effective_date.py

60 lines
1.7 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 得到数据前一天得到时间避免直接减1是错的
from datetime import datetime, timedelta
import logging
import calendar
# 获取日志记录器
logger = logging.getLogger(__name__)
# 获取前一天的日期
def effective_date(year, month, day):
try:
logger.info("开始组装获取前一天的时间")
# 拿到一个日期
date_now = year + month + day
# 转成有效时间
date = datetime.strptime(date_now, "%Y%m%d")
# 计算前一天
day_before = date - timedelta(days=1)
# 获得年月日并返回
year = day_before.year
month = day_before.month
day = day_before.day
return year, month, day
except AttributeError:
logger.exception(f"获取前一天时间失败:{AttributeError}")
# 获取后一天的日期
def is_valid_date(year, month, day):
try:
datetime(year=year, month=month, day=day)
return True
except ValueError:
return False
def get_next_day(year, month, day):
try:
if not is_valid_date(year, month, day):
raise ValueError("输入的日期无效")
current_date = datetime(year=year, month=month, day=day)
next_date = current_date + timedelta(days=1)
return next_date.year, next_date.month, next_date.day
except ValueError:
logger.exception(f"获取后一天时间失败:{ValueError}")
if __name__ == "__main__":
# 示例使用
year, month, day = 2025, 6, 5 # 闰年2月28日
next_year, next_month, next_day = get_next_day(year, month, day)
print(f"后一天是: {next_year}-{next_month}-{next_day}") # 输出: 2020-2-29