Skip to content

Commit

Permalink
feat: Add convertDateFormat function to utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Feb 3, 2024
1 parent 0050eda commit 5d77524
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/nbiatoolkit/utils/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,33 @@ def convertMillis(millis: int) -> str:
AssertionError: If the input is not an integer.
"""
assert isinstance(millis, int), "The input must be an integer"
return datetime.fromtimestamp(millis / 1000.0).strftime('%Y-%m-%d')
return datetime.fromtimestamp(millis / 1000.0).strftime('%Y-%m-%d')


def convertDateFormat(input_date: str, format: str = "%Y/%m/%d") -> str:
"""
Converts the input date to the desired format.
Args:
input_date (str): The date to be converted.
Returns:
str: The converted date in the format "YYYY/MM/DD".
Raises:
ValueError: If the input date has an invalid format.
"""
# List of possible date formats with only days, months, and years
possible_formats = [
"%Y-%m-%d", "%Y/%m/%d", "%Y%m%d", "%m/%d/%Y", "%d/%m/%Y", "%d-%m-%Y"
]
# Try parsing the input date with each possible format
for date_format in possible_formats:
try:
parsed_date = datetime.strptime(input_date, date_format)
# If parsing is successful, format the date in YYYY/MM/DD and return
return parsed_date.strftime(format)
except ValueError:
pass # If parsing fails, continue with the next format
# If none of the formats match, raise an exception or return a default value
raise ValueError("Invalid date format: {}".format(input_date))
26 changes: 24 additions & 2 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from src.nbiatoolkit.utils.parsers import clean_html, convertMillis
from src.nbiatoolkit.utils.parsers import clean_html, convertMillis, convertDateFormat
from datetime import datetime
import pytest


def test_clean_html_valid_input():
# Test case for valid input with HTML tags and special characters
html_string = "<p>This is <b>bold</b> text with special characters: &amp; &lt; &gt;</p>"
Expand Down Expand Up @@ -41,4 +43,24 @@ def test_convertMillis_invalid_input():
convertMillis(millis) # type: ignore
assert False, "Expected AssertionError"
except AssertionError as e:
assert str(e) == "The input must be an integer"
assert str(e) == "The input must be an integer"


def test_convertDateFormat_valid_input():
# Test case for valid input date in different formats
input_date = "2021-09-01"
expected_output = "2021/09/01"
assert convertDateFormat(input_date) == expected_output

def test_convertDateFormat_invalid_input():
# Test case for invalid input date format
input_date = "shoulD_fail" # Invalid format: day-month-year
with pytest.raises(ValueError) as e:
convertDateFormat(input_date)
assert str(e.value) == "Invalid date format: {}".format(input_date)

def test_convertDateFormat_default_value():
# Test case for input date with no matching format
input_date = "2021/09/01"
default_value = "N/A"
assert convertDateFormat(input_date, default_value) == default_value

0 comments on commit 5d77524

Please sign in to comment.