Améliore heure_to_iso8601. Close #1004

This commit is contained in:
Emmanuel Viennet 2024-10-28 15:46:16 +01:00
parent cc7fcded98
commit 0895d7b195
2 changed files with 51 additions and 4 deletions

View File

@ -152,7 +152,40 @@ def convert_fr_date(
raise ScoValueError("Date (j/m/a) invalide") 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), """Convert time string or time object to ISO 8601 (allows 16:03, 16h03, 16),
returns hh:mm:ss or empty string 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(":") parts = t.strip().upper().replace("H", ":").split(":")
# here we can have hh, hh:mm or hh:mm:ss # here we can have hh, hh:mm or hh:mm:ss
h = int(parts[0]) h = int(parts[0])
m = int(parts[1]) if len(parts) > 1 else 0 try:
s = int(parts[2]) if len(parts) > 2 else 0 m = int(parts[1])
return datetime.time(h, m, s).isoformat() except:
m = 0
try:
s = int(parts[2])
except:
s = 0
return "%02d:%02d:%02d" % (h, m, s)
def print_progress_bar( def print_progress_bar(

View File

@ -16,6 +16,7 @@ from app.scodoc.sco_exceptions import ScoValueError
from app.scodoc.sco_utils import convert_fr_date, heure_to_iso8601 from app.scodoc.sco_utils import convert_fr_date, heure_to_iso8601
# Dates humaines -> ISO
def test_convert_fr_date_full_date(): def test_convert_fr_date_full_date():
assert convert_fr_date("12/2/1972") == datetime.datetime(1972, 2, 12) 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") convert_fr_date("2022-02-30T00:00:00")
# ---- Heures humaines -> ISO8601
def test_heure_to_iso8601_full_time(): def test_heure_to_iso8601_full_time():
assert heure_to_iso8601("16:01:02") == "16:01:02" 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" 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(): def test_heure_to_iso8601_single_digit():
assert heure_to_iso8601("1:2:3") == "01:02:03" assert heure_to_iso8601("1:2:3") == "01:02:03"