-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adicionar método is_holiday para verificar feriados nacionais e estad…
…uais (#446) * Adicitonando is_holiday * Adicitonando is_holiday * Adicionando is_holidays * Atualizando Changelog * Lib Holidays adicionada * Adicionando instalacao de dependencias - run-tests.yml * Mudanca de nome do arquivo date_utils Para evitar conflito com a nomenclatura date de python * Removendo duplicada is_holiday no __init__ * Removendo duplicada is_holiday no __init__ * Movendo import holidays para dentro da funcao * Movendo import holidays para dentro da funcao * Adicionando __init__ na pasta de testes * Mudando o comando de rodar os testes na action * Adicionando working_directory a action run-tests.yml * Adicionando poetry run no generate report * Removendo mudancas desnecessarias Que foram feitas para debug * Atualizando o poetry lock * Arrumando run-tests.yml * Segunda tentativa run-tests.yml * Terceira tentativa * Update .github/workflows/run-tests.yml * Update .github/workflows/run-tests.yml * Update .github/workflows/run-tests.yml * regerando poetry.lock * Update brutils/date_utils.py * Update brutils/date_utils.py --------- Co-authored-by: Camila Maia <[email protected]>
- Loading branch information
1 parent
9d8c388
commit 78aa0a5
Showing
10 changed files
with
276 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from datetime import datetime | ||
from unittest import TestCase | ||
|
||
from brutils.date_utils import is_holiday | ||
|
||
|
||
class TestIsHoliday(TestCase): | ||
def test_feriados_validos(self): | ||
# Testes com feriados válidos | ||
self.assertTrue(is_holiday(datetime(2024, 1, 1))) # Ano Novo | ||
self.assertTrue( | ||
is_holiday(datetime(2024, 7, 9), uf="SP") | ||
) # Revolução Constitucionalista (SP) | ||
self.assertTrue( | ||
is_holiday(datetime(2024, 9, 7)) | ||
) # Independência do Brasil | ||
self.assertTrue(is_holiday(datetime(2025, 1, 1))) # Ano Novo | ||
|
||
def test_dias_normais(self): | ||
# Testes com dias normais | ||
self.assertFalse(is_holiday(datetime(2024, 1, 2))) # Dia normal | ||
self.assertFalse( | ||
is_holiday(datetime(2024, 7, 9), uf="RJ") | ||
) # Dia normal no RJ | ||
|
||
def test_data_invalida(self): | ||
# Testes com data inválida | ||
self.assertIsNone(is_holiday("2024-01-01")) # Formato incorreto | ||
self.assertIsNone(is_holiday(None)) # Data None | ||
|
||
def test_uf_invalida(self): | ||
# Testes com UF inválida | ||
self.assertIsNone( | ||
is_holiday(datetime(2024, 1, 1), uf="XX") | ||
) # UF inválida | ||
self.assertIsNone( | ||
is_holiday(datetime(2024, 1, 1), uf="SS") | ||
) # UF inválida | ||
|
||
def test_limite_de_datas(self): | ||
# Testes com limite de datas | ||
self.assertTrue(is_holiday(datetime(2024, 12, 25))) # Natal | ||
self.assertTrue( | ||
is_holiday(datetime(2024, 11, 15)) | ||
) # Proclamação da República | ||
|
||
def test_datas_depois_de_feriados(self): | ||
# Test data after holidays | ||
self.assertFalse(is_holiday(datetime(2024, 12, 26))) # Não é feriado | ||
self.assertFalse(is_holiday(datetime(2025, 1, 2))) # Não é feriado | ||
|
||
def test_ano_bissexto(self): | ||
# Teste ano bissexto | ||
self.assertFalse( | ||
is_holiday(datetime(2024, 2, 29)) | ||
) # Não é feriado, mas data válida | ||
# Uncomment to test non-leap year invalid date | ||
# self.assertIsNone(is_holiday(datetime(1900, 2, 29))) # Ano não bissexto, data inválida | ||
|
||
def test_data_passada_futura(self): | ||
# Teste de data passada e futura | ||
self.assertTrue(is_holiday(datetime(2023, 1, 1))) # Ano anterior | ||
self.assertTrue(is_holiday(datetime(2150, 12, 25))) # Ano futuro | ||
self.assertFalse( | ||
is_holiday(datetime(2250, 1, 2)) | ||
) # Dia normal em ano futuro | ||
|
||
def test_data_sem_uf(self): | ||
# Teste feriado nacional sem UF | ||
self.assertTrue( | ||
is_holiday(datetime(2024, 12, 25)) | ||
) # Natal, feriado nacional | ||
self.assertFalse( | ||
is_holiday(datetime(2024, 7, 9)) | ||
) # Data estadual de SP, sem UF |