<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/rcppcctz-0-1-0/"></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/12#rcppcctz_0.1.0"> Thinking inside the box </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A new version 0.1.0 of <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ</a> arrived on <a target="_blank" rel="nofollow" href="https://cloud.r-project.org/">CRAN</a> this morning. It brings a number of new or updated things, starting with new upstream code from <a target="_blank" rel="nofollow" href="https://github.com/google/cctz">CCTZ</a> as well as a few new utility functions.</p>
<p><a target="_blank" rel="nofollow" href="https://github.com/google/cctz">CCTZ</a> is <em>a C++ library for translating between absolute and civil times using the rules of a time zone</em>. In fact, it is <em>two</em> libraries. One for dealing with <em>civil time</em>: human-readable dates and times, and one for converting between between absolute and civil times via time zones. It requires only a proper C++11 compiler and the standard IANA time zone data base which standard Unix, Linux, OS X, … computers tend to have in <code>/usr/share/zoneinfo</code>. <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ</a> connects this library to R by relying on <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.html">Rcpp</a>.</p>
<p>A nice example is the <code>helloMoon()</code> function (based on an introductory example in the <a target="_blank" rel="nofollow" href="https://github.com/google/cctz">CCTZ</a> documentation) showing the time when Neil Armstrong took a small step, relative to local time in New York and Sydney:</p>
<div>
<pre><code>R><span> </span><span>library</span>(RcppCCTZ)
R><span> </span><span>helloMoon</span>(<span>verbose=</span><span>TRUE</span>)
<span>1969-07-20</span> <span>22</span>:<span>56</span>:<span>00</span> -<span>0400</span>
<span>1969-07-21</span> <span>12</span>:<span>56</span>:<span>00</span> +<span>1000</span>
                   New_York                      Sydney 
<span>&quot;1969-07-20 22:56:00 -0400&quot;</span> <span>&quot;1969-07-21 12:56:00 +1000&quot;</span> 
R><span> </span></code></pre>
</div>
<p>The new formating and parsing functions are illustrated below with default arguments for format strings and timezones. All this can be customized as usual.</p>
<div>
<pre><code>R><span> </span><span>example</span>(formatDatetime)

frmtDtR><span> </span>now &lt;-<span> </span><span>Sys.time</span>()

frmtDtR><span> </span><span>formatDatetime</span>(now)            <span># current (UTC) time, in full precision RFC3339</span>
[<span>1</span>] <span>&quot;2016-12-12T13:21:03.866711+00:00&quot;</span>

frmtDtR><span> </span><span>formatDatetime</span>(now, <span>tgttzstr=</span><span>&quot;America/New_York&quot;</span>)  <span># same but in NY</span>
[<span>1</span>] <span>&quot;2016-12-12T08:21:03.866711-05:00&quot;</span>

frmtDtR><span> </span><span>formatDatetime</span>(now +<span> </span><span>0</span>:<span>4</span>)     <span># vectorised</span>
[<span>1</span>] <span>&quot;2016-12-12T13:21:03.866711+00:00&quot;</span> <span>&quot;2016-12-12T13:21:04.866711+00:00&quot;</span> <span>&quot;2016-12-12T13:21:05.866711+00:00&quot;</span>
[<span>4</span>] <span>&quot;2016-12-12T13:21:06.866711+00:00&quot;</span> <span>&quot;2016-12-12T13:21:07.866711+00:00&quot;</span>
R><span> </span><span>example</span>(parseDatetime)

prsDttR><span> </span>ds &lt;-<span> </span><span>getOption</span>(<span>&quot;digits.secs&quot;</span>)

prsDttR><span> </span><span>options</span>(<span>digits.secs=</span><span>6</span>) <span># max value</span>

prsDttR><span> </span><span>parseDatetime</span>(<span>&quot;2016-12-07 10:11:12&quot;</span>,        <span>&quot;%Y-%m-%d %H:%M:%S&quot;</span>);   <span># full seconds</span>
[<span>1</span>] <span>&quot;2016-12-07 04:11:12 CST&quot;</span>

prsDttR><span> </span><span>parseDatetime</span>(<span>&quot;2016-12-07 10:11:12.123456&quot;</span>, <span>&quot;%Y-%m-%d %H:%M:%E*S&quot;</span>); <span># fractional seconds</span>
[<span>1</span>] <span>&quot;2016-12-07 04:11:12.123456 CST&quot;</span>

prsDttR><span> </span><span>parseDatetime</span>(<span>&quot;2016-12-07T10:11:12.123456-00:00&quot;</span>)  ## default RFC3339 format
[<span>1</span>] <span>&quot;2016-12-07 04:11:12.123456 CST&quot;</span>

prsDttR><span> </span>now &lt;-<span> </span><span>trunc</span>(<span>Sys.time</span>())

prsDttR><span> </span><span>parseDatetime</span>(<span>formatDatetime</span>(now +<span> </span><span>0</span>:<span>4</span>))               <span># vectorised</span>
[<span>1</span>] <span>&quot;2016-12-12 07:21:17 CST&quot;</span> <span>&quot;2016-12-12 07:21:18 CST&quot;</span> <span>&quot;2016-12-12 07:21:19 CST&quot;</span>
[<span>4</span>] <span>&quot;2016-12-12 07:21:20 CST&quot;</span> <span>&quot;2016-12-12 07:21:21 CST&quot;</span>

prsDttR><span> </span><span>options</span>(<span>digits.secs=</span>ds)
R></code></pre>
</div>
<p>Changes in this version are summarized here:</p>
<blockquote>
<h4>Changes in version 0.1.0 (2016-12-11)</h4>
<ul>
<li>
<p>Synchronized with <code>CCTZ</code> upstream.</p>
</li>
<li>
<p>New parsing and formating helpers for Datetime vectors</p>
</li>
<li>
<p>New parsing and formating helpers for (two) <code>double</code> vectors representing full <code>std::chrono</code> nanosecond resolutions</p>
</li>
<li>
<p>Updated documentation and examples.</p>
</li>
</ul>
</blockquote>
<p>We also have a <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/cranberries/2016/12/12#RcppCCTZ_0.1.0">diff to the previous version</a> thanks to <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/cranberries">CRANberries</a>. More details are at the <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ page</a>; code, issue tickets etc at the <a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel/rcppcctz">GitHub repository</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/12#rcppcctz_0.1.0"> 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/62YTTFrOsU0"></a></div>