Skip to content

Commit

Permalink
21953 Create datetime method to add business days to a date (bcgov#2815)
Browse files Browse the repository at this point in the history
  • Loading branch information
leodube-aot authored Jul 9, 2024
1 parent a231a2c commit 6b574a9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
16 changes: 15 additions & 1 deletion legal-api/src/legal_api/utils/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Date time utilities."""
# from datetime import datetime, timezone
import time as _time
from datetime import date, datetime as _datetime, timezone # pylint: disable=unused-import # noqa: F401, I001, I005
from datetime import date, datetime as _datetime, timedelta, timezone # pylint: disable=unused-import # noqa: E501, F401, I001, I005
# noqa: I003,I005


Expand All @@ -31,3 +31,17 @@ def utcnow(cls):
def from_date(cls, date_obj):
"""Get a datetime object from a date object."""
return datetime(date_obj.year, date_obj.month, date_obj.day)

@classmethod
def add_business_days(cls, from_date: _datetime, num_days: int):
"""Add business days to an initial date. Only accounts for weekends, not holidays."""
current_date = from_date
business_days_to_add = abs(num_days)
inc = 1 if num_days > 0 else -1
while business_days_to_add > 0:
current_date += timedelta(days=inc)
weekday = current_date.weekday()
if weekday >= 5: # sunday = 6
continue
business_days_to_add -= 1
return current_date
39 changes: 38 additions & 1 deletion legal-api/tests/unit/utils/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
# limitations under the License.

"""Tests to ensure the datetime wrappers are working as expected."""
from datetime import datetime, timezone
from datetime import date, datetime, timezone

import pytest
from freezegun import freeze_time


Expand All @@ -38,3 +39,39 @@ def test_datetime_isoformat():
iso = d.isoformat()
tz = iso[iso.find('+'):]
assert tz == '+00:00'


@pytest.mark.parametrize(
'test_name, from_date_str, num_days, expected_date_str', [
(
'ADD_WITHIN_WEEKDAYS',
'2024-06-19',
2,
'2024-06-21'
),
(
'ADD_OVER_WEEKEND',
'2024-06-19',
5,
'2024-06-26'
),
(
'SUB_WITHIN_WEEKDAYS',
'2024-06-19',
-2,
'2024-06-17'
),
(
'SUB_OVER_WEEKEND',
'2024-06-19',
-5,
'2024-06-12'
),
]
)
def test_datetime_add_business_days(test_name, from_date_str, num_days, expected_date_str):
"""Assert that business days are added to a date correctly."""
import legal_api.utils.datetime as _datetime
from_date = date.fromisoformat(from_date_str)
new_date = _datetime.datetime.add_business_days(from_date, num_days)
assert new_date == date.fromisoformat(expected_date_str)

0 comments on commit 6b574a9

Please sign in to comment.