File tree 2 files changed +60
-11
lines changed
2 files changed +60
-11
lines changed Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change 11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+ from typing import List
15
+
14
16
import datetime
15
17
import re
16
18
@@ -147,18 +149,26 @@ def format_duration(delta: datetime.timedelta) -> str:
147
149
.format (delta )
148
150
)
149
151
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
151
162
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
155
167
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" )
161
170
162
- delta_str += f"{ delta_ms } ms"
171
+ if delta_us > 0 :
172
+ output .append (f"{ delta_us // 1000 } ms" )
163
173
164
- return delta_str
174
+ return "" . join ( output )
You can’t perform that action at this time.
0 commit comments