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

Dirk Eddelbuettel edd at debian.org
Fri Jan 9 13:22:33 CET 2015


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


More information about the Rcpp-devel mailing list