Skip to content
Prev 2321 / 10988 Next

[Rcpp-devel] Datetime and DatetimeVector definitions

Hi Dirk,
Thanks for your help. I am 99% confident that there is a better method to my example below, but this is the only way i was able to pass the format string to for the datetime object.

src<-'
StringVector stringVec(dates);
int n_stringVec=stringVec.size();
std::string fmt =Rcpp::as<std::string>( format );

DatetimeVector  dateAll(n_stringVec);

std::string input;

for(int i=0;i<n_stringVec;i++){
  input = stringVec[i];
  Datetime dateTemp(input,fmt);
  dateAll[i] = dateTemp;
}

return wrap(dateAll);
'
library(inline)
cpp_DatetimeVector<-cxxfunction(signature(dates="string",format="string"),src,plugin="Rcpp")

####Create a fictional dataset
foo<-seq(as.POSIXct("1960-01-01 00:00:00"),as.POSIXct("2010-01-01 00:00:00"),by='hour')

system.time(test<-cpp_DatetimeVector(foo,"%Y-%m-%d %H:%M:%OS"))
#    user  system elapsed 
# 98.607   0.904 101.061 
system.time(test2<-strptime(foo,"%Y-%m-%d %H:%M:%OS"))
#    user  system elapsed 
# 14.089   0.261  14.501
On 19/05/2011, at 3:04 PM, Dirk Eddelbuettel wrote: