Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SCODOC_R6A06
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Paul Milleville
SCODOC_R6A06
Commits
0895d7b1
Commit
0895d7b1
authored
Oct 28, 2024
by
Emmanuel Viennet
Browse files
Options
Downloads
Patches
Plain Diff
Améliore heure_to_iso8601. Close #1004
parent
cc7fcded
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/scodoc/sco_utils.py
+43
-4
43 additions, 4 deletions
app/scodoc/sco_utils.py
tests/unit/test_sco_utils.py
+8
-0
8 additions, 0 deletions
tests/unit/test_sco_utils.py
with
51 additions
and
4 deletions
app/scodoc/sco_utils.py
+
43
−
4
View file @
0895d7b1
...
@@ -152,7 +152,40 @@ def convert_fr_date(
...
@@ -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:
...
@@ -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
(
...
...
This diff is collapsed.
Click to expand it.
tests/unit/test_sco_utils.py
+
8
−
0
View file @
0895d7b1
...
@@ -16,6 +16,7 @@ from app.scodoc.sco_exceptions import ScoValueError
...
@@ -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():
...
@@ -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():
...
@@ -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
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment