Skip to content

Commit

Permalink
Merge pull request #19 from ddelange/timestamp
Browse files Browse the repository at this point in the history
Add 'started' timestamp to output (with local timezone)
  • Loading branch information
cpcloud authored Dec 19, 2020
2 parents 7fac5ac + b83ccf6 commit 38ae41a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ $ pip install ipython-autotime

```python
In [1]: %load_ext autotime
time: 295 µs
time: 264 µs (started: 2020-12-15 11:44:36 +01:00)

In [2]: x = 1
time: 519 µs
time: 416 µs (started: 2020-12-15 11:44:45 +01:00)

In [3]: x / 0
---------------------------------------------------------------------------
Expand All @@ -23,7 +23,7 @@ ZeroDivisionError Traceback (most recent call last)
----> 1 x/0

ZeroDivisionError: division by zero
time: 79.7 ms
time: 88.7 ms (started: 2020-12-15 11:44:53 +01:00)
```

## Want to turn it off?
Expand Down
18 changes: 16 additions & 2 deletions autotime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from ._version import version as __version__

from time import strftime, localtime

try:
from time import monotonic
except ImportError:
Expand All @@ -10,21 +12,33 @@
from IPython.core.magics.execution import _format_time as format_delta


def format_timestamp(struct_time):
timestamp = strftime('%Y-%m-%d %H:%M:%S %z', struct_time)
# add colon in %z (for datetime.fromisoformat, stackoverflow.com/q/44836581)
return '{}:{}'.format(timestamp[:-2], timestamp[-2:])


class LineWatcher(object):
"""Class that implements a basic timer.
Notes
-----
* Register the `start` and `stop` methods with the IPython events API.
"""
__slots__ = ['start_time']
__slots__ = ['start_time', 'timestamp']

def start(self):
self.timestamp = localtime()
self.start_time = monotonic()

def stop(self):
delta = monotonic() - self.start_time
print(u'time: {}'.format(format_delta(delta)))
print(
u'time: {} (started: {})'.format(
format_delta(delta),
format_timestamp(self.timestamp),
)
)


timer = LineWatcher()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_autotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_full_cycle():
with tt.AssertPrints('time: '):
ip.run_cell('%load_ext autotime')

with tt.AssertPrints('time: '):
with tt.AssertPrints(' (started: '):
ip.run_cell('x = 1')

with tt.AssertNotPrints('time: '):
Expand Down

0 comments on commit 38ae41a

Please sign in to comment.