Exemplo n.º 1
0
def _validate_date(date):
    """
    check if the date is supported

    :type date: datetime.date
    :rtype: bool
    """
    min_year, max_year = min(holidays.keys()).year, max(holidays.keys()).year
    if not (min_year <= date.year <= max_year):
        raise NotImplementedError(
            'no available data for year {}, only year between [{}, {}] supported'
            .format(date.year, min_year, max_year))
Exemplo n.º 2
0
def is_workday(date):
    """
    check if one date is workday in China.
    in other words, Chinese people works at that day.
    :type date: datetime.date
    :rtype: bool
    """
    min_year, max_year = min(holidays.keys()).year, max(holidays.keys()).year
    if not (min_year <= date.year <= max_year):
        raise NotImplementedError('no available data for year {}, only year between [{}, {}] supported'.format(
            date.year, min_year, max_year))

    weekday = date.weekday()
    return bool(date in workdays.keys() or (weekday <= 4 and date not in holidays.keys()))
Exemplo n.º 3
0
def _validate_date(date):
    """
    check if the date is supported

    :type date: datetime.date | datetime.datetime
    :rtype: datetime.date | datetime.datetime
    """
    if isinstance(date, datetime.datetime):
        date = date.date()
    if not isinstance(date, datetime.date):
        raise NotImplementedError('unsupported type {}, expected type is datetime.date'.format(type(date)))
    min_year, max_year = min(holidays.keys()).year, max(holidays.keys()).year
    if not (min_year <= date.year <= max_year):
        raise NotImplementedError('no available data for year {}, only year between [{}, {}] supported'.format(
            date.year, min_year, max_year))
    return date
Exemplo n.º 4
0
def _validate_date(*dates):
    """
    check if the date(s) is supported

    :type date: datetime.date | datetime.datetime
    :rtype: datetime.date | list[datetime.date]
    """
    if len(dates) != 1:
        return list(map(_validate_date, dates))
    date = _wrap_date(dates[0])
    if not isinstance(date, datetime.date):
        raise NotImplementedError('unsupported type {}, expected type is datetime.date'.format(type(date)))
    min_year, max_year = min(holidays.keys()).year, max(holidays.keys()).year
    if not (min_year <= date.year <= max_year):
        raise NotImplementedError('no available data for year {}, only year between [{}, {}] supported'.format(
            date.year, min_year, max_year))
    return date
Exemplo n.º 5
0
def is_workday(date):
    """
    check if one date is workday in China.
    in other words, Chinese people works at that day.

    :type date: datetime.date | datetime.datetime
    :rtype: bool
    """
    date = _validate_date(date)

    weekday = date.weekday()
    return bool(date in workdays.keys() or (weekday <= 4 and date not in holidays.keys()))
Exemplo n.º 6
0
def get_holiday_detail(date):
    """
    check if one date is holiday in China,
    and return the holiday name (None if it's a normal day)

    :type date: datetime.date | datetime.datetime
    :return: holiday bool indicator, and holiday name if it's holiday related day
    :rtype: (bool, str | None)
    """
    date = _validate_date(date)
    if date in workdays.keys():
        return False, workdays[date]
    elif date in holidays.keys():
        return True, holidays[date]
    else:
        return date.weekday() > 4, None
Exemplo n.º 7
0
 def test_holiday_amount(self):
     holiday_amounts = defaultdict(int)
     for date in holidays.keys():
         if date.weekday() <= 4:
             holiday_amounts[date.year] += 1
     for date in workdays.keys():
         if date.weekday() > 4:
             holiday_amounts[date.year] -= 1
     holiday_amounts[2007] -= 2  # 07年法定节假日有13天(国庆多了两天)
     holiday_amounts[2008] -= 2  # 08年同上
     holiday_amounts[2011] += 1  # 11年要补班12年的元旦假期
     holiday_amounts[2012] -= 1  # 12年可以享受11年补班的假
     holiday_amounts[2015] -= 1  # 15年是中国人民抗日战争暨世界反法西斯战争胜利70周年,多放1天
     holiday_amounts[2020] -= 2  # 20年春节因为新型冠状病毒疫情防控,延长假期2天
     for year in range(2007, 2020 + 1):  # 06年数据少,不测了
         self.assertEqual(11, holiday_amounts[year], 'Holiday amount of year {}'.format(year))
     self.assertEqual(1, 1)