[Rcpp-devel] Fwd: Setting time zone on Rcpp::Datetime

janus Larsen janusjlarsen at gmail.com
Mon Jan 19 09:02:43 CET 2015


Thx - that did the trick. But why the 'Oh dear'?

// [[Rcpp::export]]
Rcpp::NumericVector getDT(int dt) {
  Rcpp::NumericVector res(1);
  res[0] = dt;
  res.attr("tzone") = "GMT";
  res.attr("class") = CharacterVector::create("POSIXct", "POSIXt");;
  return res;
}

On Mon, Jan 12, 2015 at 1:34 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

>
> On 12 January 2015 at 13:10, janus Larsen wrote:
> | Hi again,
> |
> | Thanks Dirk for your reply. So there is no way of doing this on the C
> side?
> |
> | 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:
> |
> | SEXP long2DateTime(long dt) {
> | SEXP result;
> | result = PROTECT(allocVector(REALSXP, 1));
> | REAL(result)[0] = dt;
> | SEXP gmt = PROTECT(allocVector(STRSXP,1));
> | SET_STRING_ELT(gmt, 0, mkChar("GMT"));
> | SEXP tzone = PROTECT(allocVector(STRSXP,1));
> | SET_STRING_ELT(tzone, 0, mkChar("tzone"));
> | setAttrib(result, tzone, gmt);
> | SEXP datetimeclass = PROTECT(allocVector(STRSXP,2));
> | SET_STRING_ELT(datetimeclass, 0, mkChar("POSIXt"));
> | SET_STRING_ELT(datetimeclass, 1, mkChar("POSIXct"));
> | setAttrib(result, R_ClassSymbol, datetimeclass);
> | UNPROTECT(4);
> | return result;
> | }
>
> Oh dear. See eg http://gallery.rcpp.org/articles/creating-xts-from-c++/ or
> the other few xts related answers on the Rcpp Gallery.
>
> Dirk
>
> | Janus
> |
> | On Fri, Jan 9, 2015 at 1:22 PM, Dirk Eddelbuettel <edd at debian.org>
> wrote:
> |
> |
> |     On 9 January 2015 at 11:50, janus Larsen wrote:
> |     | Hi,
> |     | How do I set the timezone on an Rcpp::Datetime?
> |     | Thanks in advance,
> |     | Sunaj
> |     |
> |     | This returns "1970-01-01 01:00:00 CET" (computer setting), but I
> want
> |     GMT...
> |
> |     R does the formatting in its session based on your locale.
> |
> |     That is generally the right thing:
> |
> |     | // [[Rcpp::export]]
> |     | Rcpp::Datetime test() {
> |     |   double d=0;
> |     |   return(d);
> |     | }
> |
> |     Small corrections to your code to actually export the function
> (under a
> |     safer
> |     name):
> |
> |     #include <Rcpp.h>
> |
> |     // [[Rcpp::export]]
> |     Rcpp::Datetime timetest(double d=0) {
> |       return(d);
> |     }
> |
> |     Then:
> |
> |     R> sourceCpp("/tmp/timeQ.cpp")
> |     R> timetest()                       # my default is Chicago, or -5
> |     [1] "1969-12-31 18:00:00 CST"
> |     R> as.numeric(timetest())
> |     [1] 0
> |     R> Sys.setenv("TZ"="Europe/London")
> |     R> timetest()                       # I can select another one
> |     [1] "1970-01-01 01:00:00 BST"
> |     R> Sys.setenv("TZ"="UTC")
> |     R> timetest()                       # incl UTC
> |     [1] "1970-01-01 UTC"
> |     R>
> |     R> format(timetest(), tz="America/Chicago")
> |     [1] "1969-12-31 18:00:00"
> |     R>
> |
> |     So you need to change the timezone _at the level of your app_ which
> can be
> |     as
> |     simple as writing a new Date formatter in R as per my last line.
> |
> |     Dirk
> |
> |     --
> |     http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
> |
> |
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150119/849827e0/attachment.html>


More information about the Rcpp-devel mailing list