From 19fb0ecf8f19dfb7db3239a22c9465b235f91ebf Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 23 Dec 2019 13:18:53 -0600 Subject: [PATCH] mgr/mgr_util: add to_pretty_timedelta This mirrors the Ceph C++ implementation. Signed-off-by: Sage Weil --- src/pybind/mgr/mgr_module.py | 1 + src/pybind/mgr/mgr_util.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 11e965fbda2a..93940ff0c210 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -5,6 +5,7 @@ try: except ImportError: # just for type checking pass +import datetime import logging import errno import json diff --git a/src/pybind/mgr/mgr_util.py b/src/pybind/mgr/mgr_util.py index 9edffc2e9719..2557a01b83dd 100644 --- a/src/pybind/mgr/mgr_util.py +++ b/src/pybind/mgr/mgr_util.py @@ -1,4 +1,5 @@ import contextlib +import datetime import os import socket import logging @@ -305,3 +306,18 @@ def _pairwise(iterable): for b in it: yield (a, b) a = b + +def to_pretty_timedelta(n): + if n < datetime.timedelta(seconds=120): + return str(n.seconds) + 's' + if n < datetime.timedelta(minutes=120): + return str(n.seconds // 60) + 'm' + if n < datetime.timedelta(hours=48): + return str(n.seconds // 360) + 'h' + if n < datetime.timedelta(days=14): + return str(n.days) + 'd' + if n < datetime.timedelta(days=7*12): + return str(n.days // 7) + 'w' + if n < datetime.timedelta(days=365*2): + return str(n.days // 30) + 'M' + return str(n.days // 365) + 'y' -- 2.47.3