Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c131811

Browse files
committed
Deprecate elapsed_time_to_string accepting time as milliseconds
Fixes #4862.
1 parent f4058f4 commit c131811

File tree

3 files changed

+90
-56
lines changed

3 files changed

+90
-56
lines changed

src/robot/libraries/DateTime.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def _convert_to_compact(self, seconds, millis=True):
619619
return secs_to_timestr(seconds, compact=True)
620620

621621
def _convert_to_timer(self, seconds, millis=True):
622-
return elapsed_time_to_string(seconds * 1000, include_millis=millis)
622+
return elapsed_time_to_string(seconds, include_millis=millis, seconds=True)
623623

624624
def _convert_to_timedelta(self, seconds, millis=True):
625625
return timedelta(seconds=seconds)

src/robot/utils/robottime.py

+27-10
Original file line numberDiff line numberDiff line change
@@ -403,37 +403,54 @@ def get_elapsed_time(start_time, end_time):
403403

404404

405405
def elapsed_time_to_string(elapsed: 'int|float|timedelta',
406-
include_millis: bool = True):
406+
include_millis: bool = True,
407+
seconds: bool = False):
407408
"""Converts elapsed time to format 'hh:mm:ss.mil'.
408409
409-
Elapsed time can be given as milliseconds either as an integer or
410-
as a float. Alternatively it can be given as a ``timedelta``.
410+
Elapsed time as an integer or as a float is currently considered to be
411+
milliseconds, but that will be changed to seconds in Robot Framework 8.0.
412+
Use ``seconds=True`` to change the behavior already now and to avoid the
413+
deprecation warning. An alternative is giving the elapsed time as
414+
a ``timedelta``.
411415
412416
If `include_millis` is True, '.mil' part is omitted.
417+
418+
Support for giving the elapsed time as a ``timedelta`` and the ``seconds``
419+
argument are new in Robot Framework 7.0.
413420
"""
421+
# TODO: Change the default input to seconds in RF 8.0.
414422
if isinstance(elapsed, timedelta):
415-
elapsed = round(elapsed.total_seconds() * 1000)
423+
elapsed = elapsed.total_seconds()
424+
elif not seconds:
425+
elapsed /= 1000
426+
warnings.warn("'robot.utils.elapsed_time_to_string' currently accepts "
427+
"input as milliseconds, but that will be changed to seconds "
428+
"in Robot Framework 8.0. Use 'seconds=True' to change the "
429+
"behavior already now and to avoid this warning. Alternatively "
430+
"pass the elapsed time as a 'timedelta'.")
416431
prefix = ''
417432
if elapsed < 0:
418433
prefix = '-'
419434
elapsed = abs(elapsed)
420435
if include_millis:
421-
return prefix + _elapsed_time_to_string(elapsed)
436+
return prefix + _elapsed_time_to_string_with_millis(elapsed)
422437
return prefix + _elapsed_time_to_string_without_millis(elapsed)
423438

424439

425-
def _elapsed_time_to_string(elapsed):
426-
secs, millis = divmod(round(elapsed), 1000)
440+
def _elapsed_time_to_string_with_millis(elapsed):
441+
elapsed = round(elapsed, 3)
442+
secs = int(elapsed)
443+
millis = round((elapsed - secs) * 1000)
427444
mins, secs = divmod(secs, 60)
428445
hours, mins = divmod(mins, 60)
429-
return '%02d:%02d:%02d.%03d' % (hours, mins, secs, millis)
446+
return f'{hours:02}:{mins:02}:{secs:02}.{millis:03}'
430447

431448

432449
def _elapsed_time_to_string_without_millis(elapsed):
433-
secs = round(elapsed, ndigits=-3) // 1000
450+
secs = round(elapsed)
434451
mins, secs = divmod(secs, 60)
435452
hours, mins = divmod(mins, 60)
436-
return '%02d:%02d:%02d' % (hours, mins, secs)
453+
return f'{hours:02}:{mins:02}:{secs:02}'
437454

438455

439456
def _timestamp_to_millis(timestamp, seps=None):

utest/utils/test_robottime.py

+62-45
Original file line numberDiff line numberDiff line change
@@ -270,60 +270,77 @@ def test_get_elapsed_time_negative(self):
270270

271271
def test_elapsed_time_to_string(self):
272272
for elapsed, expected in [(0, '00:00:00.000'),
273-
(0.1, '00:00:00.000'),
274-
(0.5, '00:00:00.000'),
275-
(0.501, '00:00:00.001'),
276-
(1, '00:00:00.001'),
277-
(1.5, '00:00:00.002'),
278-
(42, '00:00:00.042'),
279-
(999, '00:00:00.999'),
280-
(999.9, '00:00:01.000'),
281-
(1000, '00:00:01.000'),
282-
(1001, '00:00:01.001'),
283-
(60000, '00:01:00.000'),
284-
(600000, '00:10:00.000'),
285-
(654321, '00:10:54.321'),
286-
(660000, '00:11:00.000'),
287-
(3600000, '01:00:00.000'),
288-
(36000000, '10:00:00.000'),
289-
(360000000, '100:00:00.000'),
290-
(360000000 + 36000000 + 3600000 + 660000 + 11111,
273+
(0.0001, '00:00:00.000'),
274+
(0.00049, '00:00:00.000'),
275+
(0.00050, '00:00:00.001'),
276+
(0.00051, '00:00:00.001'),
277+
(0.001, '00:00:00.001'),
278+
(0.0015, '00:00:00.002'),
279+
(0.042, '00:00:00.042'),
280+
(0.999, '00:00:00.999'),
281+
(0.9999, '00:00:01.000'),
282+
(1.0, '00:00:01.000'),
283+
(1, '00:00:01.000'),
284+
(1.001, '00:00:01.001'),
285+
(60, '00:01:00.000'),
286+
(600, '00:10:00.000'),
287+
(654.321, '00:10:54.321'),
288+
(660, '00:11:00.000'),
289+
(3600, '01:00:00.000'),
290+
(36000, '10:00:00.000'),
291+
(360000, '100:00:00.000'),
292+
(360000 + 36000 + 3600 + 660 + 11.111,
291293
'111:11:11.111')]:
292-
td = timedelta(seconds=elapsed / 1000)
293-
assert_equal(elapsed_time_to_string(elapsed), expected, elapsed)
294-
assert_equal(elapsed_time_to_string(td), expected, elapsed)
295-
if expected != '00:00:00.000':
296-
assert_equal(elapsed_time_to_string(-1 * elapsed),
294+
assert_equal(elapsed_time_to_string(elapsed, seconds=True),
295+
expected, elapsed)
296+
assert_equal(elapsed_time_to_string(timedelta(seconds=elapsed)),
297+
expected, elapsed)
298+
if elapsed != 0:
299+
assert_equal(elapsed_time_to_string(-elapsed, seconds=True),
300+
'-' + expected, elapsed)
301+
assert_equal(elapsed_time_to_string(timedelta(seconds=-elapsed)),
297302
'-' + expected, elapsed)
298303

299304
def test_elapsed_time_to_string_without_millis(self):
300305
for elapsed, expected in [(0, '00:00:00'),
301-
(1, '00:00:00'),
302-
(500, '00:00:00'),
303-
(500.001, '00:00:01'),
304-
(999, '00:00:01'),
305-
(1000, '00:00:01'),
306-
(1499.999, '00:00:01'),
307-
(1500, '00:00:02'),
308-
(59499.9, '00:00:59'),
309-
(59500.0, '00:01:00'),
310-
(59999, '00:01:00'),
311-
(60000, '00:01:00'),
312-
(654321, '00:10:54'),
313-
(654500, '00:10:54'),
314-
(654501, '00:10:55'),
315-
(3599999, '01:00:00'),
316-
(3600000, '01:00:00'),
317-
(359999999, '100:00:00'),
318-
(360000000, '100:00:00'),
319-
(360000500, '100:00:00'),
320-
(360000500.001, '100:00:01')]:
321-
assert_equal(elapsed_time_to_string(elapsed, include_millis=False),
306+
(0.001, '00:00:00'),
307+
(0.5, '00:00:00'),
308+
(0.501, '00:00:01'),
309+
(0.999, '00:00:01'),
310+
(1.0, '00:00:01'),
311+
(1, '00:00:01'),
312+
(1.4999, '00:00:01'),
313+
(1.500, '00:00:02'),
314+
(59.4999, '00:00:59'),
315+
(59.5, '00:01:00'),
316+
(59.999, '00:01:00'),
317+
(60, '00:01:00'),
318+
(654.321, '00:10:54'),
319+
(654.500, '00:10:54'),
320+
(654.501, '00:10:55'),
321+
(3599.999, '01:00:00'),
322+
(3600, '01:00:00'),
323+
(359999.999, '100:00:00'),
324+
(360000, '100:00:00'),
325+
(360000.5, '100:00:00'),
326+
(360000.501, '100:00:01')]:
327+
assert_equal(elapsed_time_to_string(elapsed, include_millis=False,
328+
seconds=True),
322329
expected, elapsed)
323330
if expected != '00:00:00':
324-
assert_equal(elapsed_time_to_string(-1 * elapsed, False),
331+
assert_equal(elapsed_time_to_string(-1 * elapsed, False, True),
325332
'-' + expected, elapsed)
326333

334+
def test_elapsed_time_default_input_is_deprecated(self):
335+
with warnings.catch_warnings(record=True) as w:
336+
assert_equal(elapsed_time_to_string(1000), '00:00:01.000')
337+
assert_equal(str(w[0].message),
338+
"'robot.utils.elapsed_time_to_string' currently accepts input "
339+
"as milliseconds, but that will be changed to seconds in "
340+
"Robot Framework 8.0. Use 'seconds=True' to change the behavior "
341+
"already now and to avoid this warning. Alternatively pass "
342+
"the elapsed time as a 'timedelta'.")
343+
327344
def test_parse_timestamp(self):
328345
for timestamp in ['2023-09-08 23:34:45.123456',
329346
'2023-09-08T23:34:45.123456',

0 commit comments

Comments
 (0)