Skip to content

Commit

Permalink
Merge pull request #156 from ohadmata/dynamic-date-patterns-without-time
Browse files Browse the repository at this point in the history
add date only formats and patterns without day part
  • Loading branch information
ohadmata authored Jun 27, 2024
2 parents a15507c + 73bd5e4 commit 9828a5b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/shmessy/types/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class DateType(BaseType):
weight = 2
delimiters: list[str] = {"/", ".", "-", " "}
static_patterns: list[str] = ["%B %d, %Y"] # January 23, 2024
date_only_patterns: list[list[str]] = [
# Do not attach time combinations to these patterns
["%Y", "%m"], # 2022-07 | 2022 07 | 2022/07 | 2022.07
["%Y", "%b"], # 2022-Jul | 2022 Jul | 2022/Jul | 2022.Jul
["%m", "%y"], # 07-22 | 07 22 | 07/22 | 07.22
["%b", "%y"], # Jul-22 | Jul 22 | Jul/22 | Jul.22
["%b", "%Y"], # Jul-2022 | Jul 2022 | Jul/2022 | Jul.2022
]
dynamic_patterns: list[list[str]] = [
["%m", "%d", "%Y"],
["%d", "%m", "%Y"],
Expand All @@ -26,14 +34,19 @@ class DateType(BaseType):
["%d", "%b", "%Y"],
["%d", "%b", "%y"],
["%b", "%d", "%Y"],
["%Y", "%m"],
]

@classmethod
def get_patterns(cls) -> list[str]:
def get_patterns(
cls, include_date_only_patterns: Optional[bool] = True
) -> list[str]:
# The value returned cannot be set since the order is important!
input_patterns: list[list[str]] = cls.dynamic_patterns.copy()
if include_date_only_patterns:
input_patterns += cls.date_only_patterns

results: list[str] = []
for pattern in cls.dynamic_patterns:
for pattern in input_patterns:
for delimiter in cls.delimiters:
results.append(delimiter.join(pattern))
return results + cls.static_patterns
Expand Down
2 changes: 1 addition & 1 deletion src/shmessy/types/datetime_.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DatetimeType(BaseType):
@classmethod
def get_patterns(cls) -> list[str]:
result: list[str] = []
for date in DateType.get_patterns():
for date in DateType.get_patterns(include_date_only_patterns=False):
for date_time_delimiter in cls.date_time_delimiters:
for dynamic_pattern in cls.dynamic_patterns:
result.append(date + date_time_delimiter + dynamic_pattern)
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/test_date_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,17 @@ def test_dynamic_patterns(date: list[str], delimiters: set[str]):
)
def test_static_patterns(date: str):
date_type = DateType()
assert date in date_type.get_patterns()
assert date in date_type.get_patterns()


def test_get_patterns_with_date_only():
date_only_patterns: list[str] = [
"%Y-%m", "%Y %m", "%Y/%b", "%b %y", "%b.%Y", "%b-%y", "%m/%y"
]
date_type = DateType()
result_all_patterns = date_type.get_patterns()
result_date_only_patterns = date_type.get_patterns(include_date_only_patterns=False)

for p in date_only_patterns:
assert p in result_all_patterns
assert p not in result_date_only_patterns

0 comments on commit 9828a5b

Please sign in to comment.