以当天为基准,计算周六日与下一个假期间的天数差,输出下一个假期的日期与星期,并简要判断是否需要调休。

以当天为基准,计算周六日与下一个假期间的天数差,输出下一个假期的日期与星期,并简要判断是否需要调休。

码农世界 2024-05-27 前端 80 次浏览 0个评论

1. 脚本功能如标题。

预期结果:

今天是星期一,距离周六还有5天。下一个假期是端午节[06-10],当天是星期一,还剩13天,无需调休。

json格式:
{
    "today": "星期一",
    "to_weekend": 5,
    "next_holiday_name": "端午节",
    "next_holiday_date": "06-10",
    "next_holiday_week": "星期一",
    "until_holiday_days": 13,
    "rest_plan": "无需调休。",
    "other_holiday": ""
}

2. 完整代码:

# -*- coding: UTF-8 -*-
# Auther: youren.S
# createTime: 2024-05-26 20:00
# modityTime: 2024-05-27 13:00
# function: 使用python进行计算,以当天为基准,计算周六日与下一个假期间的天数差,输出下一个假期的日期与星期,并简要判断是否需要调休。
#
# find_holiday_and_days()       // 正常输出
# find_holiday_and_days("True") // json格式输出
from datetime import datetime
import re
from lunardate import LunarDate
import json
# 一年中节假日记录
# 阳历
holiday_solar = {
    "元旦节": "01-01",
    "清明节": "04-04",
    "劳动节": "05-01",
    "国庆节": "10-01"
}
# 阴历
holiday_lunar = {
    "春节": "01-01",
    "端午节": "05-05",
    "中秋节": "08-15"
}
# 阴历转阳历
holiday_lunar_to_solar = {}
# 其他不休息的节日
other_holiday = {
    "除夕": "12-30",
    "元宵节": "01-15",
    "儿童节": "06-01",
    "七夕节": "07-07"
}
# 英译汉
english_to_chinese = {
    "Monday": "星期一",
    "Tuesday": "星期二",
    "Wednesday": "星期三",
    "Thursday": "星期四",
    "Friday": "星期五",
    "Saturday": "星期六",
    "Sunday": "星期日"
}
# 获取当日日期
today = datetime.today()
year = re.split(r'\s|-', str(today))[0]
month = re.split(r'\s|-', str(today))[1]
day = re.split(r'\s|-', str(today))[2]
# 获取今天星期几
def get_today_week():
    to_week = today.weekday() # result: 0 -> 6
    weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] # 0(星期一) -> 6(星期天)
    output = weekdays[to_week]
    return output, to_week
# 查询节假日当天是星期几
def get_holiday_week(y,m,d):
    to_holiday_week = datetime(y, m, d)
    return to_holiday_week.strftime('%A')
# 阴历转换阳历
def lunar_to_solar():
    for holiday_lunar_name, holiday_lunar_date in holiday_lunar.items():
        lunar_month, lunar_day = holiday_lunar_date.split('-')
        lunar_year = year
        # 阴转阳
        solar_result = LunarDate(int(lunar_year), int(lunar_month), int(lunar_day))
        solar_date = solar_result.toSolarDate()
        # 结果转入dict
        m, d = str(solar_date).split('-')[1], str(solar_date).split('-')[2]
        data = m + '-' + d
        holiday_lunar_to_solar[holiday_lunar_name] = data
        holiday_solar[holiday_lunar_name] = data
    return holiday_lunar_to_solar, holiday_solar
# 计算假期与今天的天数差
def calculate_days_until_holiday(holiday_date_str):
    holiday_date = datetime.strptime(holiday_date_str, "%m-%d").replace(year=today.year)
    # 如果节日已经过去,尝试下一年的日期
    if holiday_date < today:
        holiday_date = holiday_date.replace(year=today.year + 1)
    return (holiday_date - today).days
# 判断今天是不是特殊节日
def otherholiday():
    other = today.strftime("%m-%d")
    for holiday, date in other_holiday.items():
        if other == date:
            if holiday == "除夕节":
                return f"今天是{holiday}, 记得好好犒劳自己。"
            elif holiday == "元宵节":
                return f"今天是{holiday}, 记得吃元宵哦。"
            elif holiday == "儿童节":
                return f"今天是{holiday}, 今天永远是明天的孩子,好好休息,永远快乐。"
            elif holiday == "七夕节":
                return f"今天是{holiday}, 记得与爱你的人和你爱的人一起吃饭。"
        return ""
# 计算工作日和周末的天数差
def week_to_weekend():
    today_week = get_today_week()[1]
    if today_week == 5:  # 今天是周六
        return "周末好好休息"
    elif today_week == 6:  # 今天是周日
        return "周末好好休息"
    else:  # 今天是周一到周五
        to_weekend = 6 - today_week - 1 # 去除周六当天
        return to_weekend
# 调休安排
def rest(holiday_name, holiday_week):
    # 逻辑推算,可能不准。
    # 3天假期
    if holiday_name == "元旦节" or holiday_name == "清明节" or holiday_name == "端午节" or holiday_name == "中秋节":
        if holiday_week == "星期三" or holiday_week == "星期五" or holiday_week == "星期一":
            return "无需调休。"
        elif holiday_week == "星期二":
            return "上一周的周六需要补班。"
        else:
            return "当周周日需要上班。"
    # 5天假期
    elif holiday_name == "劳动节":
        if holiday_week == "星期一" or holiday_week == "星期二" or holiday_week == "星期六" or holiday_week == "星期日":
            # 休息安排: 休六日一二三
            return "上一周的周日,当周周六补班。"
        elif holiday_week == "星期三" or holiday_week == "星期四":
            # 休息安排: 休三四五六日
            return "上一周的周日,当周周六补班。"
        elif holiday_week == "星期五":
            # 休息安排: 休三四五六日
            return "上一周的周日,当周周六补班。"
    # 7天国庆
    elif holiday_name == "国庆节" or holiday_name == "春节":
        if holiday_week == "星期一":
            return "上周六日补班"
        elif holiday_week == "星期二" or holiday_week == "星期三" or holiday_week == "星期四":
            return "上周日补班,下周六补班"
        elif holiday_week == "星期五" or holiday_week == "星期六":
            return "下周六日补班"
        elif holiday_week == "星期日":
            return "本周六补班,下周日补班"
# 找到距离最近的节日及天数,生成结果
def find_holiday_and_days(j=None):
    nearest_holiday = holiday_week = None
    nearest_days = float('inf')
    for holiday_name, holiday_data in lunar_to_solar()[1].items():
        days_until = calculate_days_until_holiday(holiday_data)
        if days_until < nearest_days:
            nearest_days = days_until
            nearest_holiday = holiday_name
            # 获取节假日当天是星期几
            date = holiday_solar[nearest_holiday] # 获取假日对应日期
            holiday_month, holidat_day = date.split('-')
            output = get_holiday_week(int(year), int(holiday_month), int(holidat_day))
            holiday_week = english_to_chinese[output] # 英译汉
    # 输出结果
    if j == None:
        if nearest_holiday:
            # 工作日
            if isinstance(week_to_weekend(), int):
                return (f"今天是{get_today_week()[0]},"
                        f"距离周六还有{week_to_weekend()}天。"
                        f"{otherholiday()}"
                        f"下一个假期是{nearest_holiday}[{holiday_solar[nearest_holiday]}],"
                        f"当天是{holiday_week},"
                        f"还剩{nearest_days}天,"
                        f"{rest(nearest_holiday, holiday_week)}")
            # 周六日
            else:
                return (f"今天是{get_today_week()[0]},"
                        f"{week_to_weekend()}。"
                        f"下一个假期是{nearest_holiday}[{holiday_solar[nearest_holiday]}],"
                        f"当天是{holiday_week},"
                        f"还剩{nearest_days}天,"
                        f"{rest(nearest_holiday, holiday_week)}")
        else:
            return ("error code 1, 不应该有这个结果。")
    elif j == "True":
        holiday_json = {
            "today": get_today_week()[0],
            "to_weekend": week_to_weekend(),
            "next_holiday_name": nearest_holiday,
            "next_holiday_date": holiday_solar[nearest_holiday],
            "next_holiday_week": holiday_week,
            "until_holiday_days": nearest_days,
            "rest_plan": rest(nearest_holiday, holiday_week),
            "other_holiday": otherholiday()
        }
        return json.dumps(holiday_json, ensure_ascii=False, indent=4)
    else:
        return "error code 2"
print(find_holiday_and_days())

转载请注明来自码农世界,本文标题:《以当天为基准,计算周六日与下一个假期间的天数差,输出下一个假期的日期与星期,并简要判断是否需要调休。》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,80人围观)参与讨论

还没有评论,来说两句吧...

Top