Skip to content

Commit 438c2af

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix: use half round up round instread of round half to even/banker's rounding
1 parent b851bb8 commit 438c2af

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

stac_fastapi/core/stac_fastapi/core/datetime_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Utility functions to handle datetime parsing."""
22

3+
import math
34
from datetime import datetime, timezone
45

56
from stac_fastapi.types.rfc3339 import rfc3339_str_to_datetime
@@ -23,7 +24,9 @@ def normalize(dt):
2324
return ".."
2425
dt_obj = rfc3339_str_to_datetime(dt)
2526
dt_utc = dt_obj.astimezone(timezone.utc)
26-
rounded_dt = dt_utc.replace(microsecond=round(dt_utc.microsecond / 1000) * 1000)
27+
rounded_dt = dt_utc.replace(
28+
microsecond=math.floor(dt_utc.microsecond / 1000 + 0.5) * 1000
29+
)
2730
return rounded_dt.isoformat(timespec="milliseconds").replace("+00:00", "Z")
2831

2932
if not isinstance(date_str, str):

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/datetime.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import logging
8+
import math
89
import re
910
from datetime import date
1011
from datetime import datetime as datetime_type
@@ -44,7 +45,9 @@ def normalize_datetime(dt_str):
4445
return dt_str
4546
dt_obj = rfc3339_str_to_datetime(dt_str)
4647
dt_utc = dt_obj.astimezone(timezone.utc)
47-
rounded_dt = dt_utc.replace(microsecond=round(dt_utc.microsecond / 1000) * 1000)
48+
rounded_dt = dt_utc.replace(
49+
microsecond=math.floor(dt_utc.microsecond / 1000 + 0.5) * 1000
50+
)
4851
return rounded_dt.isoformat(timespec="milliseconds").replace("+00:00", "Z")
4952

5053
result: Dict[str, Optional[str]] = {"gte": None, "lte": None}

0 commit comments

Comments
 (0)