<div dir="ltr">Hi again,<div><br></div><div>Thanks Dirk for your reply. So there is no way of doing this on the C side?</div><div><br></div><div>The thing is that I'm returning a large object that contains a lot of information (about an unstructured 3D model) and some of the fields are DateTimes. I could of cource wrap the call to the C function in an R function that then corrects the dates to GMT (or sets the timezone before calling the C function), but it just not that nice... Previously I had this code written using <RDefines.h> where I could set the timezone on the returned posIXct object:</div><div><br></div><div><div><span class="" style="white-space:pre">    </span>SEXP long2DateTime(long dt) {</div><div><span class="" style="white-space:pre">              </span>SEXP result;</div><div><span class="" style="white-space:pre">               </span>result = PROTECT(allocVector(REALSXP, 1));</div><div><span class="" style="white-space:pre">         </span>REAL(result)[0] = dt;</div><div><span class="" style="white-space:pre">              </span>SEXP gmt = PROTECT(allocVector(STRSXP,1));</div><div><span class="" style="white-space:pre">         </span>SET_STRING_ELT(gmt, 0, mkChar("GMT"));</div><div><span class="" style="white-space:pre">           </span>SEXP tzone = PROTECT(allocVector(STRSXP,1));</div><div><span class="" style="white-space:pre">               </span>SET_STRING_ELT(tzone, 0, mkChar("tzone"));</div><div><span class="" style="white-space:pre">               </span>setAttrib(result, tzone, gmt);</div><div><span class="" style="white-space:pre">             </span>SEXP datetimeclass = PROTECT(allocVector(STRSXP,2));</div><div><span class="" style="white-space:pre">               </span>SET_STRING_ELT(datetimeclass, 0, mkChar("POSIXt"));</div><div><span class="" style="white-space:pre">              </span>SET_STRING_ELT(datetimeclass, 1, mkChar("POSIXct"));</div><div><span class="" style="white-space:pre">             </span>setAttrib(result, R_ClassSymbol, datetimeclass);</div><div><span class="" style="white-space:pre">           </span>UNPROTECT(4);</div><div><span class="" style="white-space:pre">              </span>return result;</div><div><span class="" style="white-space:pre">     </span>}</div></div><div><br></div><div><br></div><div>Janus</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 9, 2015 at 1:22 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"><span class=""><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 GMT...<br>
<br>
</span>R does the formatting in its session based on your locale.<br>
<br>
That is generally the right thing:<br>
<span class=""><br>
| // [[Rcpp::export]]<br>
| Rcpp::Datetime test() {<br>
|   double d=0;<br>
|   return(d);<br>
| }<br>
<br>
</span>Small corrections to your code to actually export the function (under a 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 as<br>
simple as writing a new Date formatter in R as per my last line.<br>
<span class="HOEnZb"><font color="#888888"><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">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>