-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend/attendances): round from_time and to_time to nearest 15min (
#445) refactor(backend): move rounding into `timed.utils` chore(backend): added tests for rounding in `timed.utils`
- Loading branch information
Showing
4 changed files
with
87 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from datetime import time, timedelta | ||
|
||
import pytest | ||
|
||
from timed.utils import round_time, round_timedelta | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("unrounded", "rounded"), | ||
[ | ||
( | ||
time(5, 14, 11), | ||
time(5, 15, 0), | ||
), | ||
( | ||
time(12, 15, 0), | ||
time(12, 15, 0), | ||
), | ||
( | ||
time(14, 37, 20), | ||
time(14, 30, 0), | ||
), | ||
( | ||
time(22, 37, 35), | ||
time(22, 45, 0), | ||
), | ||
( | ||
time(23, 56, 35), | ||
time(0, 0, 0), | ||
), | ||
], | ||
) | ||
def test_round_time(unrounded: time, rounded: time) -> None: | ||
assert round_time(unrounded) == rounded | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("unrounded", "rounded"), | ||
[ | ||
( | ||
timedelta(hours=5, minutes=17), | ||
timedelta(hours=5, minutes=15), | ||
), | ||
( | ||
timedelta(hours=2, minutes=37, seconds=25), | ||
timedelta(hours=2, minutes=30), | ||
), | ||
( | ||
timedelta(hours=3, minutes=37, seconds=48), | ||
timedelta(hours=3, minutes=45), | ||
), | ||
], | ||
) | ||
def test_round_timedelta(unrounded: timedelta, rounded: timedelta) -> None: | ||
assert round_timedelta(unrounded) == rounded |
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
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,25 @@ | ||
from datetime import time, timedelta | ||
|
||
|
||
def round_time(t: time) -> time: | ||
"""Round a datetime.time to the nearest 15min.""" | ||
minutes = round((t.minute * 60 + t.second) / (60 * 15)) * 15 | ||
hours = t.hour | ||
|
||
if minutes > time.max.minute: | ||
hours += 1 | ||
minutes = 0 | ||
|
||
if hours > time.max.hour: | ||
hours = 0 | ||
|
||
return time( | ||
hours, | ||
minutes, | ||
0, | ||
) | ||
|
||
|
||
def round_timedelta(td: timedelta) -> timedelta: | ||
"""Round a datetime.timedelta to the nearest 15min.""" | ||
return timedelta(seconds=max(15 * 60, round(td.seconds / (15 * 60)) * 15 * 60)) |