{"id":4980,"date":"2024-02-06T12:57:34","date_gmt":"2024-02-06T11:57:34","guid":{"rendered":"https:\/\/pmortensen.eu\/world2\/?p=4980"},"modified":"2026-08-01T14:10:21","modified_gmt":"2026-08-01T13:10:21","slug":"snippet-for-absolute-timestamps-with-subsecond-resolution-in-iso-8601-format-on-the-linux-command","status":"publish","type":"post","link":"https:\/\/pmortensen.eu\/world2\/2024\/02\/06\/snippet-for-absolute-timestamps-with-subsecond-resolution-in-iso-8601-format-on-the-linux-command\/","title":{"rendered":"Linux command-line snippet for absolute timestamps with subsecond resolution in ISO 8601 format"},"content":{"rendered":"<p>Use<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\ndate '+%FT%T.%N'\r\n<\/pre>\n<p><!-- Old:  date +%FT%T_%N_ns --><\/p>\n<p>Sample output:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n2024-02-05T22:27:08.064175582\r\n<\/pre>\n<p>That is sortable output, but a resolution of 1\/100 second is probably sufficient. The output can be rounded by postprocessing. For example (properly rounded, not just truncated),<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\ndate '+%FT%T.%N' | perl -nle '($secRes, $ns) = split(\/\\.\/); $rounded = sprintf(&quot;%.2f&quot;, $ns \/ 1000000000); $rounded =~ s\/^0\/\/; printf(&quot;$secRes$rounded&quot;)'\r\n<\/pre>\n<p>Sample output:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n2024-02-05T22:27:08.06\r\n<\/pre>\n<p>Notes:<\/p>\n<ul>\n<li>The enclosing single quotes can be left out, but they allow inserting <strong><em>spaces<\/em><\/strong> in the output<\/li>\n<li>It does not work in all cases (in all default shells, for example, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bash_%28Unix_shell%29\">Bash<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/BusyBox\">BusyBox<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Almquist_shell#Dash\">Dash<\/a> (used, for example, in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Debian\">Debian<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Linux_Mint#LMDE\">LMDE<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ubuntu_%28operating_system%29\">Ubuntu<\/a>, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Raspberry_Pi\">Raspberry Pi<\/a>), <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bourne_shell\">Bourne shell<\/a> (executable `sh`), and <a href=\"https:\/\/en.wikipedia.org\/wiki\/KornShell\">KornShell<\/a> (executable `ksh`)), but it should work in Bash and Dash.<\/li>\n<\/ul>\n<h3>A sample application<\/h3>\n<p>For example, it can be used to get absolute timestamps for start and end of a command (not just the run time in seconds):<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nclear; echo ; echo &quot;Start time: $(date '+%FT%T.%N' | perl -nle '($secRes, $ns) = split(\/\\.\/); $rounded = sprintf(&quot;%.2f&quot;, $ns \/ 1000000000); $rounded =~ s\/^0\/\/; printf(&quot;$secRes$rounded&quot;)')&quot;  ; echo\r\ngit log -i -S&quot;Q10 Max&quot; --all  --  keyboards\r\nclear; echo ; echo &quot;End time:    $(date '+%FT%T.%N' | perl -nle '($secRes, $ns) = split(\/\\.\/); $rounded = sprintf(&quot;%.2f&quot;, $ns \/ 1000000000); $rounded =~ s\/^0\/\/; printf(&quot;$secRes$rounded&quot;)')&quot;  ; echo\r\n<\/pre>\n<p>The absolute time may be important later. For example, to know\/document when a particular command was run (and how long it took).<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/hackerpublicradio.org\/about.html#about\">Hacker Public Radio<\/a> <a href=\"https:\/\/hackerpublicradio.org\/eps\/hpr4678\/index.html\">episode 4678: <em>High resolution elapsed time in shell scripts<\/em><\/a>. For example, at 12 min 59 secs, near <em>&#8220;we can use the date command&#8221;<\/em> in the transcript. <a href=\"https:\/\/hub.hackerpublicradio.org\/ccdn.php?filename=\/eps\/hpr4678\/hpr4678.ogg\">Direct download URL<\/a> (save it with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ogg\"><em>.ogg<\/em><\/a> filename extension, for example, <em>HPR4678.ogg<\/em>)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Use Sample output: That is sortable output, but a resolution of 1\/100 second is probably sufficient. The output can be rounded by postprocessing. For example (properly rounded, not just truncated), Sample output: Notes: The enclosing single quotes can be left &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/pmortensen.eu\/world2\/2024\/02\/06\/snippet-for-absolute-timestamps-with-subsecond-resolution-in-iso-8601-format-on-the-linux-command\/\"> <span class=\"screen-reader-text\">Linux command-line snippet for absolute timestamps with subsecond resolution in ISO 8601 format<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23,46],"tags":[],"_links":{"self":[{"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/posts\/4980"}],"collection":[{"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/comments?post=4980"}],"version-history":[{"count":15,"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/posts\/4980\/revisions"}],"predecessor-version":[{"id":6765,"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/posts\/4980\/revisions\/6765"}],"wp:attachment":[{"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/media?parent=4980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/categories?post=4980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pmortensen.eu\/world2\/wp-json\/wp\/v2\/tags?post=4980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}