The timestamp operator adds a timestamp to each item emitted. The following figure shows the marble diagram of this operator:
Figure 9.23: The timestamp operator
Its prototype is the following:
Observable.timestamp(self, scheduler=None)
An alternative scheduler can be provided with the scheduler parameter. If no scheduler parameter is provided, then the timeout scheduler is used.
Here is an example of the timestamp scheduler:
numbers = Subject()
numbers.timestamp().subscribe(
on_next = lambda i: print("on_next {}: {}".format(i.value, i.timestamp)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)
numbers.on_next(1)
time.sleep(0.1)
numbers.on_next(2)
time.sleep(0.1)
numbers.on_next(3)
time.sleep(0.1)
numbers.on_next(4)
This gives the following result: