MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / humanize_time_delta

Function humanize_time_delta

tensorpack/utils/utils.py:26–65  ·  view source on GitHub ↗

Humanize timedelta given in seconds Args: sec (float): time difference in seconds. Must be positive. Returns: str - time difference as a readable string Example: .. code-block:: python print(humanize_time_delta(1)) # 1 se

(sec)

Source from the content-addressed store, hash-verified

24
25
26def humanize_time_delta(sec):
27 """Humanize timedelta given in seconds
28
29 Args:
30 sec (float): time difference in seconds. Must be positive.
31
32 Returns:
33 str - time difference as a readable string
34
35 Example:
36
37 .. code-block:: python
38
39 print(humanize_time_delta(1)) # 1 second
40 print(humanize_time_delta(60 + 1)) # 1 minute 1 second
41 print(humanize_time_delta(87.6)) # 1 minute 27 seconds
42 print(humanize_time_delta(0.01)) # 0.01 seconds
43 print(humanize_time_delta(60 * 60 + 1)) # 1 hour 1 second
44 print(humanize_time_delta(60 * 60 * 24 + 1)) # 1 day 1 second
45 print(humanize_time_delta(60 * 60 * 24 + 60 * 2 + 60*60*9 + 3)) # 1 day 9 hours 2 minutes 3 seconds
46 """
47 if sec < 0:
48 logger.warn("humanize_time_delta() obtains negative seconds!")
49 return "{:.3g} seconds".format(sec)
50 if sec == 0:
51 return "0 second"
52 time = datetime(2000, 1, 1) + timedelta(seconds=int(sec))
53 units = ['day', 'hour', 'minute', 'second']
54 vals = [int(sec // 86400), time.hour, time.minute, time.second]
55 if sec < 60:
56 vals[-1] = sec
57
58 def _format(v, u):
59 return "{:.3g} {}{}".format(v, u, "s" if v > 1 else "")
60
61 ans = []
62 for v, u in zip(vals, units):
63 if v > 0:
64 ans.append(_format(v, u))
65 return " ".join(ans)
66
67
68@contextmanager

Callers 3

_trigger_epochMethod · 0.85
logMethod · 0.85
main_loopMethod · 0.85

Calls 4

_formatFunction · 0.85
formatMethod · 0.80
appendMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected