File tree 1 file changed +10
-3
lines changed
1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change 21
21
# really be a big deal.
22
22
reDuration = re .compile (r'^([0-9]{1,5}(h|m|s|ms)){1,4}$' )
23
23
24
+ # maxDuration_ms is the maximum duration that GEP-2257 can support, in milliseconds.
25
+ maxDuration_ms = (((99999 * 3600 ) + (59 * 60 ) + 59 ) * 1_000 ) + 999
26
+
24
27
def parse_duration (duration ) -> datetime .timedelta :
25
28
"""
26
29
Parse GEP-2257 Duration format to a datetime.timedelta object.
@@ -65,6 +68,13 @@ def format_duration(delta: datetime.timedelta) -> str:
65
68
if delta == datetime .timedelta (0 ):
66
69
return "0s"
67
70
71
+ # Check range early.
72
+ if delta < datetime .timedelta (0 ):
73
+ raise ValueError ("Cannot express negative durations in GEP-2257: {}" .format (delta ))
74
+
75
+ if delta > datetime .timedelta (milliseconds = maxDuration_ms ):
76
+ raise ValueError ("Cannot express durations longer than 99999h59m59s999ms in GEP-2257: {}" .format (delta ))
77
+
68
78
# durationpy.to_str() is happy to use floating-point seconds, which
69
79
# GEP-2257 is _not_ happy with. So start by peeling off any microseconds
70
80
# from our delta.
@@ -90,8 +100,5 @@ def format_duration(delta: datetime.timedelta) -> str:
90
100
91
101
delta_str += f"{ delta_ms } ms"
92
102
93
- if not reDuration .match (delta_str ):
94
- raise ValueError ("Invalid duration format: {}" .format (durationpy .to_str (delta )))
95
-
96
103
return delta_str
97
104
You can’t perform that action at this time.
0 commit comments