MCPcopy Index your code
hub / github.com/ipython/ipython / _format_time

Function _format_time

IPython/core/magics/execution.py:1687–1723  ·  view source on GitHub ↗

Formats the timespan in a human readable form

(timespan, precision=3)

Source from the content-addressed store, hash-verified

1685
1686
1687def _format_time(timespan, precision=3):
1688 """Formats the timespan in a human readable form"""
1689
1690 if timespan >= 60.0:
1691 # we have more than a minute, format that in a human readable form
1692 # Idea from http://snipplr.com/view/5713/
1693 parts = [("d", 60 * 60 * 24), ("h", 60 * 60), ("min", 60), ("s", 1)]
1694 time = []
1695 leftover = timespan
1696 for suffix, length in parts:
1697 value = int(leftover / length)
1698 if value > 0:
1699 leftover = leftover % length
1700 time.append("%s%s" % (str(value), suffix))
1701 if leftover < 1:
1702 break
1703 return " ".join(time)
1704
1705 # Unfortunately characters outside of range(128) can cause problems in
1706 # certain terminals.
1707 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1708 # Try to prevent crashes by being more secure than it needs to
1709 # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set.
1710 units = ["s", "ms", "us", "ns"] # the safe value
1711 if hasattr(sys.stdout, "encoding") and sys.stdout.encoding:
1712 try:
1713 "μ".encode(sys.stdout.encoding)
1714 units = ["s", "ms", "μs", "ns"]
1715 except:
1716 pass
1717 scaling = [1, 1e3, 1e6, 1e9]
1718
1719 if timespan > 0.0:
1720 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1721 else:
1722 order = 3
1723 return "%.*g %s" % (precision, timespan * scaling[order], units[order])

Callers 2

__str__Method · 0.85
timeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…