Skip to content

Commit

Permalink
Add ns and us support to robot time string (#4491)
Browse files Browse the repository at this point in the history
Time string now supports microsecond and nanosecond inputs like `1us`.

Co-authored-by: Mikko Kujala <mikko.kujala@fi.abb.com>
  • Loading branch information
mikkuja and mikkuja committed Oct 10, 2022
1 parent 92f6233 commit 32fbc1b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ Time string 10 s 10
0 s 0
0.1 millisecond 0.0001
0.123456789 ms 0.000123456789
123 μs 0.000123
1 ns 1E-9

Number as string 10 10
0.5 0.5
-1 -1
0 0
0.123456789 0.123456789
1E-9 1E-9

Number ${42} 42
${3.14} 3.14
${-0.5} -0.5
${0} 0
${0.123456789} 0.123456789
${1E-9} 1E-9

Timer 00:00:00.000 0
00:00:00.001 0.001
Expand Down
2 changes: 2 additions & 0 deletions src/robot/libraries/DateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@
- ``minutes``, ``minute``, ``mins``, ``min``, ``m``
- ``seconds``, ``second``, ``secs``, ``sec``, ``s``
- ``milliseconds``, ``millisecond``, ``millis``, ``ms``
- ``microseconds``, ``microsecond``, ``us``, ``μs``
- ``nanoseconds``, ``nanosecond``, ``ns``
When returning a time string, it is possible to select between ``verbose``
and ``compact`` representations using ``result_format`` argument. The verbose
Expand Down
13 changes: 9 additions & 4 deletions src/robot/utils/robottime.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _time_string_to_secs(timestr):
timestr = _normalize_timestr(timestr)
if not timestr:
return None
millis = secs = mins = hours = days = 0
nanos = micros = millis = secs = mins = hours = days = 0
if timestr[0] == '-':
sign = -1
timestr = timestr[1:]
Expand All @@ -87,7 +87,9 @@ def _time_string_to_secs(timestr):
temp = []
for c in timestr:
try:
if c == 'x': millis = float(''.join(temp)); temp = []
if c == 'n': nanos = float(''.join(temp)); temp = []
elif c == 'u': micros = float(''.join(temp)); temp = []
elif c == 'x': millis = float(''.join(temp)); temp = []
elif c == 's': secs = float(''.join(temp)); temp = []
elif c == 'm': mins = float(''.join(temp)); temp = []
elif c == 'h': hours = float(''.join(temp)); temp = []
Expand All @@ -97,12 +99,15 @@ def _time_string_to_secs(timestr):
return None
if temp:
return None
return sign * (millis/1000 + secs + mins*60 + hours*60*60 + days*60*60*24)
return sign * (nanos/1E9 + micros/1E6 + millis/1000 + secs +
mins*60 + hours*60*60 + days*60*60*24)


def _normalize_timestr(timestr):
timestr = normalize(timestr)
for specifier, aliases in [('x', ['millisecond', 'millisec', 'millis',
for specifier, aliases in [('n', ['nanosecond', 'ns']),
('u', ['microsecond', 'us', 'μs']),
('x', ['millisecond', 'millisec', 'millis',
'msec', 'ms']),
('s', ['second', 'sec']),
('m', ['minute', 'min']),
Expand Down
14 changes: 14 additions & 0 deletions utest/utils/test_robottime.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def test_timestr_to_secs_with_time_string(self):
('0day 0hour 0minute 0seconds 0millisecond', 0)]:
assert_equal(timestr_to_secs(inp), exp, inp)

def test_timestr_to_secs_with_time_string_ns_accuracy(self):
for input, expected in [("1 us", 1E-6),
("1 μs", 1E-6),
("1 microsecond", 1E-6),
("1 microseconds", 1E-6),
("2 us", 2E-6),
("1 ns", 1E-9),
("1 nanosecond", 1E-9),
("1 nanoseconds", 1E-9),
("2 ns", 2E-9),
("-100 ns", -100E-9),
("1.2us", 1.2E-6)]:
assert_equal(timestr_to_secs(input, round_to=9), expected)

def test_timestr_to_secs_with_timer_string(self):
for inp, exp in [('00:00:00', 0),
('00:00:01', 1),
Expand Down

0 comments on commit 32fbc1b

Please sign in to comment.