generated from uw-it-aca/django-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exclude weekend days from the checkin search offset
- Loading branch information
Showing
4 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2024 UW-IT, University of Washington | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from django.test import TestCase | ||
from compass.utils import weekdays_before | ||
from datetime import date | ||
|
||
|
||
class TestUtils(TestCase): | ||
def test_weekdays_before(self): | ||
val = weekdays_before(date(2024, 4, 19), offset_days=3) | ||
self.assertEqual(str(val), '2024-04-16', 'Friday, offset=3') | ||
|
||
val = weekdays_before(date(2024, 4, 20), offset_days=3) | ||
self.assertEqual(str(val), '2024-04-17', 'Saturday, offset=3') | ||
|
||
val = weekdays_before(date(2024, 4, 21), offset_days=3) | ||
self.assertEqual(str(val), '2024-04-17', 'Sunday, offset=3') | ||
|
||
val = weekdays_before(date(2024, 4, 22), offset_days=1) | ||
self.assertEqual(str(val), '2024-04-19', 'Monday, offset=1') | ||
|
||
val = weekdays_before(date(2024, 4, 22), offset_days=2) | ||
self.assertEqual(str(val), '2024-04-18', 'Monday, offset=2') | ||
|
||
val = weekdays_before(date(2024, 4, 22), offset_days=3) | ||
self.assertEqual(str(val), '2024-04-17', 'Monday, offset=3') | ||
|
||
# Bad offsets | ||
val = weekdays_before(date(2024, 4, 22), offset_days=None) | ||
self.assertEqual(str(val), '2024-04-22', 'Monday, offset=None') | ||
|
||
val = weekdays_before(date(2024, 4, 22), offset_days=-5) | ||
self.assertEqual(str(val), '2024-04-22', 'Monday, offset=-5') | ||
|
||
val = weekdays_before(date(2024, 4, 22), offset_days=0) | ||
self.assertEqual(str(val), '2024-04-22', 'Monday, offset=0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2024 UW-IT, University of Washington | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from datetime import date, timedelta | ||
|
||
|
||
def weekdays_before(end_date, offset_days=3): | ||
""" | ||
Adjust a start_date to exclude weekend days from the passed offset_days. | ||
""" | ||
start_date = end_date | ||
if offset_days is not None and offset_days > 0: | ||
start_date = end_date - timedelta(days=offset_days) | ||
|
||
day = start_date | ||
actual_offset = 1 if (day.weekday() == 6) else 0 | ||
while day < end_date: | ||
actual_offset += 1 | ||
if day.weekday() >= 5: | ||
actual_offset += 1 | ||
day = day + timedelta(days=1) | ||
|
||
return end_date - timedelta(days=actual_offset) |