<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Thx - that did the trick. But why the 'Oh dear'?<div><br></div><div><div>// [[Rcpp::export]]</div><div>Rcpp::NumericVector getDT(int dt) {</div><div>Â Rcpp::NumericVector res(1);</div><div>Â res[0] = dt;</div><div>Â res.attr("tzone") = "GMT";Â </div><div>Â res.attr("class") = CharacterVector::create("POSIXct", "POSIXt");;</div><div>Â return res;</div><div>}</div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 12, 2015 at 1:34 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br>
On 12 January 2015 at 13:10, janus Larsen wrote:<br>
| Hi again,<br>
|<br>
| Thanks Dirk for your reply. So there is no way of doing this on the C side?<br>
|<br>
| The thing is that I'm returning a large object that contains a lot of<br>
| information (about an unstructured 3D model) and some of the fields are<br>
| DateTimes. I could of cource wrap the call to the C function in an R function<br>
| that then corrects the dates to GMT (or sets the timezone before calling the C<br>
| function), but it just not that nice... Previously I had this code written<br>
| using <RDefines.h> where I could set the timezone on the returned posIXct<br>
| object:<br>
|<br>
| SEXP long2DateTime(long dt) {<br>
| SEXP result;<br>
| result = PROTECT(allocVector(REALSXP, 1));<br>
| REAL(result)[0] = dt;<br>
| SEXP gmt = PROTECT(allocVector(STRSXP,1));<br>
| SET_STRING_ELT(gmt, 0, mkChar("GMT"));<br>
| SEXP tzone = PROTECT(allocVector(STRSXP,1));<br>
| SET_STRING_ELT(tzone, 0, mkChar("tzone"));<br>
| setAttrib(result, tzone, gmt);<br>
| SEXP datetimeclass = PROTECT(allocVector(STRSXP,2));<br>
| SET_STRING_ELT(datetimeclass, 0, mkChar("POSIXt"));<br>
| SET_STRING_ELT(datetimeclass, 1, mkChar("POSIXct"));<br>
| setAttrib(result, R_ClassSymbol, datetimeclass);<br>
| UNPROTECT(4);<br>
| return result;<br>
| }<br>
<br>
</div></div>Oh dear. See eg <a href="http://gallery.rcpp.org/articles/creating-xts-from-c++/" target="_blank">http://gallery.rcpp.org/articles/creating-xts-from-c++/</a> or<br>
the other few xts related answers on the Rcpp Gallery.<br>
<span><font color="#888888"><br>
Dirk<br>
</font></span><div><div><br>
| Janus<br>
|<br>
| On Fri, Jan 9, 2015 at 1:22 PM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>> wrote:<br>
|<br>
|<br>
|Â Â Â On 9 January 2015 at 11:50, janus Larsen wrote:<br>
|Â Â Â | Hi,<br>
|   | How do I set the timezone on an Rcpp::Datetime?<br>
|Â Â Â | Thanks in advance,<br>
|Â Â Â | Sunaj<br>
|Â Â Â |<br>
|   | This returns "1970-01-01 01:00:00 CET" (computer setting), but I want<br>
|Â Â Â GMT...<br>
|<br>
|Â Â Â R does the formatting in its session based on your locale.<br>
|<br>
|Â Â Â That is generally the right thing:<br>
|<br>
|Â Â Â | // [[Rcpp::export]]<br>
|Â Â Â | Rcpp::Datetime test() {<br>
|Â Â Â | Â double d=0;<br>
|Â Â Â | Â return(d);<br>
|Â Â Â | }<br>
|<br>
|Â Â Â Small corrections to your code to actually export the function (under a<br>
|Â Â Â safer<br>
|Â Â Â name):<br>
|<br>
|Â Â Â #include <Rcpp.h><br>
|<br>
|Â Â Â // [[Rcpp::export]]<br>
|Â Â Â Rcpp::Datetime timetest(double d=0) {<br>
|Â Â Â Â return(d);<br>
|Â Â Â }<br>
|<br>
|Â Â Â Then:<br>
|<br>
|Â Â Â R> sourceCpp("/tmp/timeQ.cpp")<br>
|Â Â Â R> timetest()Â Â Â Â Â Â Â Â Â Â Â Â # my default is Chicago, or -5<br>
|Â Â Â [1] "1969-12-31 18:00:00 CST"<br>
|Â Â Â R> as.numeric(timetest())<br>
|Â Â Â [1] 0<br>
|Â Â Â R> Sys.setenv("TZ"="Europe/London")<br>
|Â Â Â R> timetest()Â Â Â Â Â Â Â Â Â Â Â Â # I can select another one<br>
|Â Â Â [1] "1970-01-01 01:00:00 BST"<br>
|Â Â Â R> Sys.setenv("TZ"="UTC")<br>
|Â Â Â R> timetest()Â Â Â Â Â Â Â Â Â Â Â Â # incl UTC<br>
|Â Â Â [1] "1970-01-01 UTC"<br>
|Â Â Â R><br>
|Â Â Â R> format(timetest(), tz="America/Chicago")<br>
|Â Â Â [1] "1969-12-31 18:00:00"<br>
|Â Â Â R><br>
|<br>
|Â Â Â So you need to change the timezone _at the level of your app_ which can be<br>
|Â Â Â as<br>
|Â Â Â simple as writing a new Date formatter in R as per my last line.<br>
|<br>
|Â Â Â Dirk<br>
|<br>
|Â Â Â --<br>
|Â Â Â <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
|<br>
|<br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
</div></div></blockquote></div><br></div>
</div></div></div><br></div>