<p>A new version 0.1.0 of <a href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ</a> arrived on <a 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 href="https://github.com/google/cctz">CCTZ</a> as well as a few new utility functions.</p>
<p><a href="https://github.com/google/cctz">CCTZ</a> is a C++ library for translating between absolute and civil times using the rules of a time zone. In fact, it is two libraries. One for dealing with civil time: 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 href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ</a> connects this library to R by relying on <a 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 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 href="http://dirk.eddelbuettel.com/cranberries/2016/12/12#RcppCCTZ_0.1.0">diff to the previous version</a> thanks to <a href="http://dirk.eddelbuettel.com/cranberries">CRANberries</a>. More details are at the <a href="http://dirk.eddelbuettel.com/code/rcpp.cctz.html">RcppCCTZ page</a>; code, issue tickets etc at the <a href="https://github.com/eddelbuettel/rcppcctz">GitHub repository</a>.</p>
<p>
This post by <a href="http://dirk.eddelbuettel.com/">Dirk Eddelbuettel</a> originated on his <a 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>