獲取每年所有的週六日

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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章