获取每年所有的周六日

import arrow
from datetime import datetime
import time

"""
判断是否是闰年,返回一年的天数
"""


def is_leap_year(years):
    if (years % 4 == 0 and years % 100 != 0) or (years % 400 == 0):  # 判断是否是闰年
        days_sum = 366
        return days_sum
    else:
        days_sum = 365
        return days_sum


"""
获取一年的所有日期
"""


def get_all_day_per_year(years):
    start_date = '%s-1-1' % years
    a = 0
    all_date_list = []
    days_sum = is_leap_year(int(years))
    while a < days_sum:
        # 获取某个日期n天后的日期
        b = arrow.get(start_date).shift(days=a).format("YYYY-MM-DD")
        a += 1
        all_date_list.append(b)
    return all_date_list


"""
获取今年包括今天前的所有日期
"""


def get_all_day_this_year():
    # 获取今天的日期
    today_time = time.strftime("%Y%m%d", time.localtime(time.time()))
    years = today_time[0:4]

    start_date = '%s-1-1' % years
    a = 0
    all_date_list = []
    days_sum = is_leap_year(int(years))

    while a < days_sum:
        # 获取某个日期n天后的日期
        b = arrow.get(start_date).shift(days=a).format("YYYYMMDD")
        if int(b) > int(today_time):
            return all_date_list
        a += 1
        all_date_list.append(b)

    return all_date_list


"""
判断某天是周几
"""


def judge_weekday(time_date):
    # 0:周一 1:周二 2:周三 3:周四 4:周五 5:周六 6:周日
    today = datetime.strptime(time_date, "%Y%m%d")
    return True if int(today.weekday()) == 5 or int(today.weekday()) == 6 else False


# 获取所有周末
def judge_weekend_days(date_list):
    all_weekend_days = []
    for i in date_list:
        if judge_weekday(i):
            all_weekend_days.append(i)
    return all_weekend_days


if __name__ == '__main__':
    # 获取今年到今天为止的所有周末
    all_date = get_all_day_this_year()
    weekends = judge_weekend_days(all_date)
    # 还需要减去法定加班的日期
    print(weekends)

 

你可能感兴趣的:(python,时间)