MCPcopy Create free account
hub / github.com/ipython/ipython / _format_time

Function _format_time

IPython/core/magics/execution.py:1475–1512  ·  view source on GitHub ↗

Formats the timespan in a human readable form

(timespan, precision=3)

Source from the content-addressed store, hash-verified

1473 return text[:colon], int(text[colon+1:])
1474
1475def _format_time(timespan, precision=3):
1476 """Formats the timespan in a human readable form"""
1477
1478 if timespan >= 60.0:
1479 # we have more than a minute, format that in a human readable form
1480 # Idea from http://snipplr.com/view/5713/
1481 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1482 time = []
1483 leftover = timespan
1484 for suffix, length in parts:
1485 value = int(leftover / length)
1486 if value > 0:
1487 leftover = leftover % length
1488 time.append(u'%s%s' % (str(value), suffix))
1489 if leftover < 1:
1490 break
1491 return " ".join(time)
1492
1493
1494 # Unfortunately the unicode 'micro' symbol can cause problems in
1495 # certain terminals.
1496 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1497 # Try to prevent crashes by being more secure than it needs to
1498 # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set.
1499 units = [u"s", u"ms",u'us',"ns"] # the save value
1500 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1501 try:
1502 u'\xb5'.encode(sys.stdout.encoding)
1503 units = [u"s", u"ms",u'\xb5s',"ns"]
1504 except:
1505 pass
1506 scaling = [1, 1e3, 1e6, 1e9]
1507
1508 if timespan > 0.0:
1509 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1510 else:
1511 order = 3
1512 return u"%.*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