Printing relative timestamps with annotate-output.

There is a simple tool in Linux that timestamps every single line of output, it is called annotate-output and is contained in the devscripts package. Sample output is like this:

€ annotate-output +%T ping localhost -c3
09:25:24 I: Started ping localhost -c3
09:25:24 O: PING localhost (127.0.0.1) 56(84) bytes of data.
09:25:24 O: 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.053 ms
09:25:25 O: 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.045 ms
09:25:26 O: 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.056 ms
09:25:26 O:
09:25:26 O: --- localhost ping statistics ---
09:25:26 O: 3 packets transmitted, 3 received, 0% packet loss, time 1999ms
09:25:26 O: rtt min/avg/max/mdev = 0.045/0.051/0.056/0.007 ms
09:25:26 I: Finished with exitcode 0

Every single line is prepended with its timestamp. However sometimes it comes in handy when relative timestamps would be printed, but unfortunately there is no command line flag for that. There is a little trick to fool annotate-output to print its output with relative timestamps and fiddles a bit with the timezone setting passed to it. The trick is to set the current timezone such that it compensates for the current hour and minute:

TZ=$( TZ="UTC" date -d "now" "+UTC+%H:%M:%S" ) annotate-output +%T ping localhost -c3
00:00:00 I: Started ping localhost -c3
00:00:00 O: PING localhost (127.0.0.1) 56(84) bytes of data.
00:00:00 O: 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.049 ms
00:00:01 O: 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.052 ms
00:00:02 O: 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.050 ms
00:00:02 O:
00:00:02 O: --- localhost ping statistics ---
00:00:02 O: 3 packets transmitted, 3 received, 0% packet loss, time 1998ms
00:00:02 O: rtt min/avg/max/mdev = 0.049/0.050/0.052/0.006 ms
00:00:02 I: Finished with exitcode 0