Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions brutils/date.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import re
from typing import Union

from num2words import num2words

from brutils.data.enums.months import MonthsEnum


def convert_date_to_text(date: str) -> Union[str, None]:
def convert_date_to_text(date: str) -> str | None:
"""
Converts a given date in Brazilian format (dd/mm/yyyy) to its textual representation.

Expand All @@ -24,9 +23,7 @@ def convert_date_to_text(date: str) -> Union[str, None]:
"""
pattern = re.compile(r"\d{2}/\d{2}/\d{4}")
if not re.match(pattern, date):
raise ValueError(
"Date is not a valid date. Please pass a date in the format dd/mm/yyyy."
)
return None

day_str, month_str, year_str = date.split("/")
day = int(day_str)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def test_convert_date_to_text(self):
self.assertIsNone(convert_date_to_text("29/02/2019"), None)

# Invalid date pattern.
self.assertRaises(ValueError, convert_date_to_text, "Invalid")
self.assertRaises(ValueError, convert_date_to_text, "25/1/2020")
self.assertRaises(ValueError, convert_date_to_text, "1924/08/20")
self.assertRaises(ValueError, convert_date_to_text, "5/09/2020")
self.assertIsNone(convert_date_to_text("Invalid"))
self.assertIsNone(convert_date_to_text("25/1/2020"))
self.assertIsNone(convert_date_to_text("1924/08/20"))
self.assertIsNone(convert_date_to_text("5/09/2020"))

self.assertEqual(
convert_date_to_text("29/02/2020"),
Expand Down