forked from brazilian-utils/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_utils.py
More file actions
58 lines (41 loc) · 1.79 KB
/
Copy pathdate_utils.py
File metadata and controls
58 lines (41 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from datetime import datetime
from typing import Union
import holidays
def is_holiday(target_date: datetime, uf: str = None) -> Union[bool, None]:
"""
Checks if the given date is a national or state holiday in Brazil.
This function takes a date as a `datetime` object and an optional UF (Unidade Federativa),
returning a boolean value indicating whether the date is a holiday or `None` if the date or
UF are invalid.
The method does not handle municipal holidays.
Args:
target_date (datetime): The date to be checked.
uf (str, optional): The state abbreviation (UF) to check for state holidays.
If not provided, only national holidays will be considered.
Returns:
bool | None: Returns `True` if the date is a holiday, `False` if it is not,
or `None` if the date or UF are invalid.
Note:
The function logic should be implemented using the `holidays` library.
For more information, refer to the documentation at: https://pypi.org/project/holidays/
Usage Examples:
>>> from datetime import datetime
>>> is_holiday(datetime(2024, 1, 1))
True
>>> is_holiday(datetime(2024, 1, 2))
False
>>> is_holiday(datetime(2024, 3, 2), uf="SP")
False
>>> is_holiday(datetime(2024, 12, 25), uf="RJ")
True
"""
if not isinstance(target_date, datetime):
return None
valid_ufs = holidays.Brazil().subdivisions
if uf is not None and uf not in valid_ufs:
return None
national_holidays = holidays.Brazil(years=target_date.year)
if uf is None:
return target_date in national_holidays
state_holidays = holidays.Brazil(prov=uf, years=target_date.year)
return target_date in state_holidays