[rtems-central commit] content: Add duration()

Sebastian Huber sebh at rtems.org
Tue May 9 13:45:27 UTC 2023


Module:    rtems-central
Branch:    master
Commit:    6bde06e0e252491c3db9c2fbe52e6cc7ddac081c
Changeset: http://git.rtems.org/rtems-central/commit/?id=6bde06e0e252491c3db9c2fbe52e6cc7ddac081c

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri May  5 14:41:20 2023 +0200

content: Add duration()

---

 rtemsspec/content.py            | 14 ++++++++++++++
 rtemsspec/tests/test_content.py | 10 +++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

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"



More information about the vc mailing list