Skip to content

Commit 38ae41a

Browse files
authored
Merge pull request #19 from ddelange/timestamp
Add 'started' timestamp to output (with local timezone)
2 parents 7fac5ac + b83ccf6 commit 38ae41a

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ $ pip install ipython-autotime
1111

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

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

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

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

2929
## Want to turn it off?

autotime/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from ._version import version as __version__
44

5+
from time import strftime, localtime
6+
57
try:
68
from time import monotonic
79
except ImportError:
@@ -10,21 +12,33 @@
1012
from IPython.core.magics.execution import _format_time as format_delta
1113

1214

15+
def format_timestamp(struct_time):
16+
timestamp = strftime('%Y-%m-%d %H:%M:%S %z', struct_time)
17+
# add colon in %z (for datetime.fromisoformat, stackoverflow.com/q/44836581)
18+
return '{}:{}'.format(timestamp[:-2], timestamp[-2:])
19+
20+
1321
class LineWatcher(object):
1422
"""Class that implements a basic timer.
1523
1624
Notes
1725
-----
1826
* Register the `start` and `stop` methods with the IPython events API.
1927
"""
20-
__slots__ = ['start_time']
28+
__slots__ = ['start_time', 'timestamp']
2129

2230
def start(self):
31+
self.timestamp = localtime()
2332
self.start_time = monotonic()
2433

2534
def stop(self):
2635
delta = monotonic() - self.start_time
27-
print(u'time: {}'.format(format_delta(delta)))
36+
print(
37+
u'time: {} (started: {})'.format(
38+
format_delta(delta),
39+
format_timestamp(self.timestamp),
40+
)
41+
)
2842

2943

3044
timer = LineWatcher()

tests/test_autotime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_full_cycle():
1010
with tt.AssertPrints('time: '):
1111
ip.run_cell('%load_ext autotime')
1212

13-
with tt.AssertPrints('time: '):
13+
with tt.AssertPrints(' (started: '):
1414
ip.run_cell('x = 1')
1515

1616
with tt.AssertNotPrints('time: '):

0 commit comments

Comments
 (0)