6

On this system:

$ cat /etc/issue
Ubuntu 14.04.4 LTS \n \l
$ uname -a
Linux mypc 3.19.0-56-generic #62~14.04.1-Ubuntu SMP Fri Mar 11 11:03:33 UTC 2016 i686 i686 i686 GNU/Linux
$ apt-show-versions -r moreutils
liblist-moreutils-perl:i386/trusty 0.33-1build3 uptodate
moreutils:i386/trusty 0.50 uptodate

... I can use date with a nanosecond precision, no problem:

$ date +'%Y%m%d%H%M%S'
20160327133441
$ date +'%Y%m%d%H%M%S%N'
20160327133446582969969

... however, if I use the same format string with moreutils' ts, the nanosecond precision fails:

$ ping google.com | ts '%Y%m%d%H%M%S%N'
20160327133829%N PING google.com (216.58.209.110) 56(84) bytes of data.
20160327133829%N 64 bytes from arn06s07-in-f14.1e100.net (216.58.209.110): icmp_seq=1 ttl=54 time=32.0 ms
20160327133830%N 64 bytes from arn06s07-in-f14.1e100.net (216.58.209.110): icmp_seq=2 ttl=54 time=31.6 ms
^C

Any way to get ts to show nano- (or micro-) second precision when prepending timestamps to stdin?

8

Starting from moreutils 0.31 %.S specifier is available, use it instead of %S :

ping google.com | ts '%Y%m%d-%H:%M:%.S'
20160327-15:01:11.361885 PING google.com (216.58.209.206) 56(84) bytes of data.
20160327-15:01:11.362056 64 bytes from bud02s22-in-f206.1e100.net (216.58.209.206): icmp_seq=1 ttl=57 time=26.3 ms
20160327-15:01:12.314243 64 bytes from bud02s22-in-f206.1e100.net (216.58.209.206): icmp_seq=2 ttl=57 time=26.2 ms
20160327-15:01:13.315651 64 bytes from bud02s22-in-f206.1e100.net (216.58.209.206): icmp_seq=3 ttl=57 time=26.3 ms
|improve this answer|||||
  • That's nice - great there is a solution; cheers @Tagwint! – sdaau Mar 27 '16 at 12:10
  • Just want to add a reference: manpages.debian.org/testing/moreutils/ts.1.en.html "In addition to the regular strftime conversion specifications, "%.S" and "%.s" and "%.T" are like "%S" and "%s" and "%T", but provide subsecond resolution" – Jan-Philip Gehrcke Dec 9 '19 at 10:43
2

As of v0.62 the '%T' specifier also comes in a high-resolution variety:

$ for i in {1..5}; do hostname; done | ts '[%F %.T]'
[2018-05-27 17:25:14.153490] quark
[2018-05-27 17:25:14.153753] quark
[2018-05-27 17:25:14.153779] quark
[2018-05-27 17:25:14.153798] quark
[2018-05-27 17:25:14.153816] quark
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.