Skip to content

Commit

Permalink
feat: also support date as argument for get_edifact_format_version (#3
Browse files Browse the repository at this point in the history
)

* feat: also support date as argument for `get_edifact_format_version`

not only datetime

* support 3.9, too
  • Loading branch information
hf-kklein authored Aug 1, 2024
1 parent 6d7cc86 commit 0eeb2ab
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions dev_requirements/requirements-type_check.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# specific requirements for the tox type_check environment
mypy
types-pytz
11 changes: 6 additions & 5 deletions dev_requirements/requirements-type_check.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# SHA1:7983aaa01d64547827c20395d77e248c41b2572f
#
# This file is autogenerated by pip-compile-multi
# To update, run:
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile-multi
# pip-compile requirements-type_check.in
#
mypy==1.11.1
# via -r dev_requirements/requirements-type_check.in
# via -r requirements-type_check.in
mypy-extensions==1.0.0
# via mypy
types-pytz==2024.1.0.20240417
# via -r requirements-type_check.in
typing-extensions==4.10.0
# via mypy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [] # add all the dependencies here
dependencies = ["pytz"] # add all the dependencies here
dynamic = ["readme", "version"]

[project.urls]
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
#
# pip-compile pyproject.toml
#
pytz==2024.1
# via efoli (pyproject.toml)
9 changes: 8 additions & 1 deletion src/efoli/edifact_format_version.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""contains the EdifactFormatVersion enum"""

import datetime
from typing import Union

import pytz

from .strenum import StrEnum

_berlin = pytz.timezone("Europe/Berlin")


class EdifactFormatVersion(StrEnum):
"""
Expand All @@ -25,7 +30,7 @@ def __str__(self) -> str:
return self.value


def get_edifact_format_version(key_date: datetime.datetime) -> EdifactFormatVersion:
def get_edifact_format_version(key_date: Union[datetime.datetime, datetime.date]) -> EdifactFormatVersion:
"""
Retrieves the appropriate Edifact format version applicable for the given key date.
Expand All @@ -36,6 +41,8 @@ def get_edifact_format_version(key_date: datetime.datetime) -> EdifactFormatVers
:param key_date: The date for which the Edifact format version is to be determined.
:return: The Edifact format version valid for the specified key date.
"""
if not isinstance(key_date, datetime.datetime) and isinstance(key_date, datetime.date):
key_date = _berlin.localize(datetime.datetime.combine(key_date, datetime.time(0, 0, 0, 0)))
format_version_thresholds = [
(datetime.datetime(2021, 9, 30, 22, 0, 0, 0, tzinfo=datetime.timezone.utc), EdifactFormatVersion.FV2104),
(datetime.datetime(2022, 9, 30, 22, 0, 0, 0, tzinfo=datetime.timezone.utc), EdifactFormatVersion.FV2110),
Expand Down
11 changes: 9 additions & 2 deletions unittests/test_edifact_format_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import date, datetime, timezone

import pytest

Expand All @@ -11,7 +11,12 @@
pytest.param(
datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
EdifactFormatVersion.FV2104,
id="Anything before 2021-04-01",
id="Anything before 2021-04-01 (datetime)",
),
pytest.param(
date(2021, 1, 1),
EdifactFormatVersion.FV2104,
id="Anything before 2021-04-01 (date)",
),
pytest.param(datetime(2021, 5, 1, 0, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2104),
pytest.param(datetime(2021, 10, 1, 0, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2110),
Expand All @@ -28,6 +33,8 @@
pytest.param(datetime(2024, 9, 30, 22, 0, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2410),
pytest.param(datetime(2025, 3, 31, 22, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2410),
pytest.param(datetime(2025, 4, 3, 22, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2504),
pytest.param(date(2025, 4, 3), EdifactFormatVersion.FV2410),
pytest.param(date(2025, 4, 4), EdifactFormatVersion.FV2504),
pytest.param(datetime(2025, 9, 30, 22, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2510),
pytest.param(
datetime(2050, 10, 1, 0, 0, 0, tzinfo=timezone.utc), EdifactFormatVersion.FV2510
Expand Down

0 comments on commit 0eeb2ab

Please sign in to comment.