diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index c2f6f2dde..20c351dae 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -152,7 +152,40 @@ def convert_fr_date( raise ScoValueError("Date (j/m/a) invalide") -def heure_to_iso8601(t: str | datetime.time = None) -> str: +# Various implementations of heure_to_iso8601 with timing: +# def heure_to_iso8601_v0(t: str | datetime.time = None) -> str: # 682 nsec per loop +# """Convert time string or time object to ISO 8601 (allows 16:03, 16h03, 16), +# returns hh:mm:ss or empty string +# """ +# if isinstance(t, datetime.time): +# return t.isoformat() +# if not t: +# return "" +# parts = t.strip().upper().replace("H", ":").split(":") +# # here we can have hh, hh:mm or hh:mm:ss +# h = int(parts[0]) +# m = int(parts[1]) if len(parts) > 1 and parts[1].strip() else 0 +# s = int(parts[2]) if len(parts) > 2 and parts[1].strip() else 0 +# return datetime.time(h, m, s).isoformat() + + +# def heure_to_iso8601_v1(t: str | datetime.time = None) -> str: # 581 nsec per loop +# """Convert time string or time object to ISO 8601 (allows 16:03, 16h03, 16), +# returns hh:mm:ss or empty string +# """ +# if isinstance(t, datetime.time): +# return t.isoformat() +# if not t: +# return "" +# parts = t.strip().upper().replace("H", ":").split(":") +# # here we can have hh, hh:mm or hh:mm:ss +# h = int(parts[0]) +# m = int(parts[1]) if len(parts) > 1 and parts[1].strip() else 0 +# s = int(parts[2]) if len(parts) > 2 and parts[1].strip() else 0 +# return "%02d:%02d:%02d" % (h, m, s) + + +def heure_to_iso8601(t: str | datetime.time = None) -> str: # 500 nsec per loop """Convert time string or time object to ISO 8601 (allows 16:03, 16h03, 16), returns hh:mm:ss or empty string """ @@ -163,9 +196,15 @@ def heure_to_iso8601(t: str | datetime.time = None) -> str: parts = t.strip().upper().replace("H", ":").split(":") # here we can have hh, hh:mm or hh:mm:ss h = int(parts[0]) - m = int(parts[1]) if len(parts) > 1 else 0 - s = int(parts[2]) if len(parts) > 2 else 0 - return datetime.time(h, m, s).isoformat() + try: + m = int(parts[1]) + except: + m = 0 + try: + s = int(parts[2]) + except: + s = 0 + return "%02d:%02d:%02d" % (h, m, s) def print_progress_bar( diff --git a/tests/unit/test_sco_utils.py b/tests/unit/test_sco_utils.py index 4f0baa16d..afcd365bf 100644 --- a/tests/unit/test_sco_utils.py +++ b/tests/unit/test_sco_utils.py @@ -16,6 +16,7 @@ from app.scodoc.sco_exceptions import ScoValueError from app.scodoc.sco_utils import convert_fr_date, heure_to_iso8601 +# Dates humaines -> ISO def test_convert_fr_date_full_date(): assert convert_fr_date("12/2/1972") == datetime.datetime(1972, 2, 12) @@ -47,6 +48,9 @@ def test_convert_fr_date_invalid_iso_format(): convert_fr_date("2022-02-30T00:00:00") +# ---- Heures humaines -> ISO8601 + + def test_heure_to_iso8601_full_time(): assert heure_to_iso8601("16:01:02") == "16:01:02" @@ -59,6 +63,10 @@ def test_heure_to_iso8601_hour_only(): assert heure_to_iso8601("16") == "16:00:00" +def test_heure_to_iso8601_hour_h(): + assert heure_to_iso8601("16h") == "16:00:00" + + def test_heure_to_iso8601_single_digit(): assert heure_to_iso8601("1:2:3") == "01:02:03"