<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>