Skip to content

Commit b3d4b57

Browse files
authored
Merge pull request kubernetes-client#2269 from kflynn/flynn/dev/gep-2257-tweaks
More GEP-2257 tweaks
2 parents 1bc5b5e + f05c642 commit b3d4b57

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

examples/duration-gep2257.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/python3
2+
# -*- coding:utf-8 -*-
3+
4+
"""
5+
This example uses kubernetes.utils.duration to parse and display
6+
a GEP-2257 duration string (you can find the full specification at
7+
https://gateway-api.sigs.k8s.io/geps/gep-2257/).
8+
9+
Good things to try:
10+
>>> python examples/duration-gep2257.py 1h
11+
Duration: 1h
12+
>>> python examples/duration-gep2257.py 3600s
13+
Duration: 1h
14+
>>> python examples/duration-gep2257.py 90m
15+
Duration: 1h30m
16+
>>> python examples/duration-gep2257.py 30m1h10s5s
17+
Duration: 1h30m15s
18+
>>> python examples/duration-gep2257.py 0h0m0s0ms
19+
Duration: 0s
20+
>>> python examples/duration-gep2257.py -5m
21+
ValueError: Invalid duration format: -5m
22+
>>> python examples/duration-gep2257.py 1.5h
23+
ValueError: Invalid duration format: 1.5h
24+
"""
25+
26+
import sys
27+
28+
from kubernetes.utils import duration
29+
30+
def main():
31+
if len(sys.argv) != 2:
32+
print("Usage: {} <duration>".format(sys.argv[0]))
33+
sys.exit(1)
34+
35+
dur = duration.parse_duration(sys.argv[1])
36+
print("Duration: %s" % duration.format_duration(dur))
37+
38+
if __name__ == "__main__":
39+
main()

kubernetes/utils/duration.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from typing import List
15+
1416
import datetime
1517
import re
1618

@@ -147,18 +149,26 @@ def format_duration(delta: datetime.timedelta) -> str:
147149
.format(delta)
148150
)
149151

150-
# Second short-circuit.
152+
# After that, do the usual div & mod tree to take seconds and get hours,
153+
# minutes, and seconds from it.
154+
secs = int(delta.total_seconds())
155+
156+
output: List[str] = []
157+
158+
hours = secs // 3600
159+
if hours > 0:
160+
output.append(f"{hours}h")
161+
secs -= hours * 3600
151162

152-
delta -= datetime.timedelta(microseconds=delta_us)
153-
delta_ms = delta_us // 1000
154-
delta_str = durationpy.to_str(delta)
163+
minutes = secs // 60
164+
if minutes > 0:
165+
output.append(f"{minutes}m")
166+
secs -= minutes * 60
155167

156-
if delta_ms > 0:
157-
# We have milliseconds to add back in. Make sure to not have a leading
158-
# "0" if we have no other duration components.
159-
if delta == datetime.timedelta(0):
160-
delta_str = ""
168+
if secs > 0:
169+
output.append(f"{secs}s")
161170

162-
delta_str += f"{delta_ms}ms"
171+
if delta_us > 0:
172+
output.append(f"{delta_us // 1000}ms")
163173

164-
return delta_str
174+
return "".join(output)

0 commit comments

Comments
 (0)