<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/nanotime-0-0-1-new-package-for-nanosecond-resolution-time-for-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://dirk.eddelbuettel.com/blog/2016/12/16#nanotime_0.0.1"> Thinking inside the box </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R has excellent tools for dates and times. The <code>Date</code> and <code>POSIXct</code> classes (as well as the ‘wide’ representation in <code>POSIXlt</code>) are versatile, and a lot of useful tooling has been built around them.</p>
<p>However, <code>POSIXct</code> is implemented as a <code>double</code> with fractional seconds since the epoch. Given the 53 bits accuracy, it leaves just a bit less than <em>microsecond</em> resolution. Which is good enough for most things.</p>
<p>But more and more performance measurements, latency statistics, … are now measured more finely, and we need <em>nanosecond</em> resolution. For which commonly an <code>integer64</code> is used to represent nanoseconds since the epoch.</p>
<p>And while R does not a <em>native</em> type for this, the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=bit64">bit64</a> package by <a target="_blank" rel="nofollow" href="https://github.com/joehl">Jens Oehlschlägel</a> offers a performant one implemented as a lightweight S3 class. So this package uses this <code>integer64</code> class, along with two helper functions for parsing and formatting, respectively, at nano-second resolution from the <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ</a> package which wraps the <a target="_blank" rel="nofollow" href="https://github.com/google/cctz">CCTZ library</a> from Google. CCTZ is a modern C++11 library extending the (C++11-native) <code>chrono</code> type.</p>
<h3>Examples</h3>
<h4>Simple Parsing and Arithmetic</h4>
<div>
<pre><code>R><span> </span>x &lt;-<span> </span><span>nanotime</span>(<span>&quot;1970-01-01T00:00:00.000000001+00:00&quot;</span>)
R><span> </span><span>print</span>(x)
integer64
[<span>1</span>] <span>1</span>
R><span> </span><span>format</span>(x)
[<span>1</span>] <span>&quot;1970-01-01T00:00:00.000000001+00:00&quot;</span>
R><span> </span><span>cat</span>(<span>&quot;x+1 is: &quot;</span>)
x<span>+1</span> is:<span> </span>R><span> </span>x &lt;-<span> </span>x +<span> </span><span>1</span>
R><span> </span><span>print</span>(x)
integer64
[<span>1</span>] <span>2</span>
R><span> </span><span>format</span>(x)
[<span>1</span>] <span>&quot;1970-01-01T00:00:00.000000002+00:00&quot;</span>
R></code></pre>
</div>
<h4>Vectorised</h4>
<div>
<pre><code>R><span> </span><span>options</span>(<span>&quot;width&quot;</span>=<span>60</span>)
R><span> </span>v &lt;-<span> </span><span>nanotime</span>(<span>Sys.time</span>()) +<span> </span><span>1</span>:<span>5</span>
R><span> </span>v
integer64
[<span>1</span>] <span>1481505724483583001</span> <span>1481505724483583002</span>
[<span>3</span>] <span>1481505724483583003</span> <span>1481505724483583004</span>
[<span>5</span>] <span>1481505724483583005</span>
R><span> </span><span>format</span>(v)
[<span>1</span>] <span>&quot;2016-12-12T01:22:04.483583001+00:00&quot;</span>
[<span>2</span>] <span>&quot;2016-12-12T01:22:04.483583002+00:00&quot;</span>
[<span>3</span>] <span>&quot;2016-12-12T01:22:04.483583003+00:00&quot;</span>
[<span>4</span>] <span>&quot;2016-12-12T01:22:04.483583004+00:00&quot;</span>
[<span>5</span>] <span>&quot;2016-12-12T01:22:04.483583005+00:00&quot;</span>
R><span> </span></code></pre>
</div>
<h4>Use with <code>zoo</code></h4>
<div>
<pre><code>R><span> </span>z &lt;-<span> </span><span>zoo</span>(<span>cbind</span>(<span>A=</span><span>1</span>:<span>5</span>, <span>B=</span><span>5</span>:<span>1</span>), v)
R><span> </span><span>options</span>(<span>&quot;nanotimeFormat&quot;</span>=<span>&quot;%d %b %H:%M:%E*S&quot;</span>)  ## override default
R><span> </span>z
                          A B
<span>12</span> Dec <span>01</span>:<span>47</span>:<span>55.812513001</span> <span>1</span> <span>5</span>
<span>12</span> Dec <span>01</span>:<span>47</span>:<span>55.812513002</span> <span>2</span> <span>4</span>
<span>12</span> Dec <span>01</span>:<span>47</span>:<span>55.812513003</span> <span>3</span> <span>3</span>
<span>12</span> Dec <span>01</span>:<span>47</span>:<span>55.812513004</span> <span>4</span> <span>2</span>
<span>12</span> Dec <span>01</span>:<span>47</span>:<span>55.812513005</span> <span>5</span> <span>1</span>
R><span> </span></code></pre>
</div>
<h3>Technical Details</h3>
<p>The <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=bit64">bit64</a> package (by <a target="_blank" rel="nofollow" href="https://github.com/joehl">Jens Oehlschlägel</a>) supplies the <code>integer64</code> type used to store the nanosecond resolution time as (positive or negative) offsets to the epoch of January 1, 1970. The <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ</a> package supplies the formatting and parsing routines based on the (modern C++) library <a target="_blank" rel="nofollow" href="https://github.com/google/cctz">CCTZ</a> from Google.</p>
<h3>Status</h3>
<p>Version 0.0.1 has now been released. It works with some other packages, notably <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=zoo">zoo</a> and <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=data.table">data.table</a>.</p>
<p>It (at least currently) requires <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=RcppCCTZ">RcppCCTZ</a> to parse and format nanosecond resolution time objects from / to text — and this package is on Linux and OS X only due to its use of system time zoneinfo. The requirement could be relaxed in the future by rewriting formating and parsing code. Contributions are welcome.</p>
<h3>Installation</h3>
<p>The package is not yet on <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">CRAN</a>. Until it gets there, or to install the development versions, it can also be installed via a standard</p>
<div>
<pre><code><span>install.packages</span>(<span>&quot;RcppCCTZ&quot;</span>)   <span># need 0.1.0 or later</span>
remotes::<span>install_github</span>(<span>&quot;eddelbuettel/nanotime&quot;</span>)  </code></pre>
</div>
<p>If you prefer <code>install.packages()</code> (as I do), use the version from the <a target="_blank" rel="nofollow" href="http://ghrr.github.io/drat/">ghrr drat</a>:</p>
<div>
<pre><code><span>install.packages</span>(<span>&quot;drat&quot;</span>)       <span># easier repo access + creation</span>
drat:::<span>add</span>(<span>&quot;ghrr&quot;</span>)             <span># make it known</span>
<span>install.packages</span>(<span>&quot;nanotime&quot;</span>)   <span># install it</span></code></pre>
</div>
<p>If and when it gets to <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">CRAN</a> you will be able to do</p>
<div>
<pre><code><span>install.packages</span>(<span>&quot;nanotime&quot;</span>)</code></pre>
</div>
<h3>Contact</h3>
<p>For questions or comments use the <a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel/nanotime/issues">issue tracker</a> off the <a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel/nanotime">GitHub repo</a>.</p>
<p>
This post by <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/">Dirk Eddelbuettel</a> originated on his <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/blog/">Thinking inside the box</a> blog. Please report excessive re-aggregation in third-party for-profit settings.</p>
<p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://dirk.eddelbuettel.com/blog/2016/12/16#nanotime_0.0.1"> Thinking inside the box </a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YWbVU4Nel6Y"></a></div>