File tree 2 files changed +29
-0
lines changed 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -131,3 +131,23 @@ def json_serialize(obj):
131
131
return str (obj )
132
132
except :
133
133
raise TypeError (repr (obj ) + ' is not JSON serializable' )
134
+
135
+
136
+ def format_time (seconds ):
137
+ """
138
+ format seconds to time string
139
+ :param seconds: seconds, in float format
140
+ :return: formatted time string
141
+ """
142
+ h = int (seconds // 3600 )
143
+ m = int (seconds % 3600 // 60 )
144
+ s = seconds % 60
145
+
146
+ if h :
147
+ time_str = '{:d}h {:d}min {:.02f}s' .format (h , m , s )
148
+ elif m :
149
+ time_str = '{:d}min {:.02f}s' .format (m , s )
150
+ else :
151
+ time_str = '{:.02f}s' .format (s )
152
+
153
+ return time_str
Original file line number Diff line number Diff line change @@ -115,3 +115,12 @@ def test_json_serialize():
115
115
assert json_serialize (Decimal ('11.0' )) == '11.0'
116
116
assert json_serialize (b'123' ) == "b'123'"
117
117
assert json_serialize (11 ) == '11'
118
+
119
+
120
+ def test_format_time ():
121
+ assert format_time (60 ) == '1min 0.00s'
122
+ assert format_time (60.1 ) == '1min 0.10s'
123
+ assert format_time (3600 ) == '1h 0min 0.00s'
124
+ assert format_time (3660.1121 ) == '1h 1min 0.11s'
125
+
126
+ assert format_time (12.2132145 ) == '12.21s'
You can’t perform that action at this time.
0 commit comments