diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2023-05-05 14:41:20 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2023-05-08 21:58:56 +0200 |
commit | 6bde06e0e252491c3db9c2fbe52e6cc7ddac081c (patch) | |
tree | 0f8b3dc44740d13c8cd78a6e071cd9af4a809cd5 | |
parent | specview.py: Improve item enablement (diff) | |
download | rtems-central-6bde06e0e252491c3db9c2fbe52e6cc7ddac081c.tar.bz2 |
content: Add duration()
-rw-r--r-- | rtemsspec/content.py | 14 | ||||
-rw-r--r-- | rtemsspec/tests/test_content.py | 10 |
2 files changed, 23 insertions, 1 deletions
diff --git a/rtemsspec/content.py b/rtemsspec/content.py index f804bbaf..980b46a1 100644 --- a/rtemsspec/content.py +++ b/rtemsspec/content.py @@ -1206,3 +1206,17 @@ def get_integer_type(value: int) -> str: """ power = 2**max(math.ceil(math.log2(math.floor(math.log2(value)) + 1)), 3) return f"uint{power}_t" + + +def duration(value: float) -> str: + """ Converts a duration in seconds into a value with unit string. """ + assert value >= 0.0 + if value == 0.0: + return "0s" + if value < 1e-6: + return f"{value * 1e9:.3f}ns" + if value < 1e-3: + return f"{value * 1e6:.3f}μs" + if value < 1.0: + return f"{value * 1e3:.3f}ms" + return f"{value:.3f}s" diff --git a/rtemsspec/tests/test_content.py b/rtemsspec/tests/test_content.py index f794c96b..73d06c6d 100644 --- a/rtemsspec/tests/test_content.py +++ b/rtemsspec/tests/test_content.py @@ -27,7 +27,7 @@ import os import pytest -from rtemsspec.content import Content, enabled_by_to_exp, \ +from rtemsspec.content import Content, duration, enabled_by_to_exp, \ ExpressionMapper, PythonExpressionMapper, to_camel_case @@ -330,3 +330,11 @@ def test_enabled_by_to_python_exp(): to_python_exp({"foo": "bar"}) with pytest.raises(ValueError): to_python_exp({"foo": "bar", "bla": "blub"}) + + +def test_duration(): + assert duration(1.0) == "1.000s" + assert duration(0.0) == "0s" + assert duration(0.001) == "1.000ms" + assert duration(0.0009) == "900.000μs" + assert duration(0.0000009) == "900.000ns" |