[Rcpp-devel] Datetime and DatetimeVector definitions

Jason Lessels jlessels at gmail.com
Thu May 19 15:27:06 CEST 2011


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:

> 
> Hi Jason,
> 
> On 19 May 2011 at 11:00, Jason Lessels wrote:
> | Hi,
> | I have a very limited understanding of the class definition, but i was wondering if I could make a request for a minor change in relation to the two classes Datetime and the extension DatetimeVector. I have been successfully using Datetime class, passing both the date string and the format string, as this is allowed with the following definition;
> | 
> | 00047	 Datetime::Datetime(const std::string &s, const std::string &fmt) {
> | 00048                 Rcpp::Function strptime("strptime");    // we cheat and call strptime() from R
> | 00049                 Rcpp::Function asPOSIXct("as.POSIXct"); // and we need to convert to POSIXct
> | 00050                 m_dt = Rcpp::as<double>(asPOSIXct(strptime(s, fmt)));
> | 00051                 update_tm();
> | 00052     }
> | 
> | however the DatetimeVector definition uses;
> | 
> | 00027         DatetimeVector::DatetimeVector(SEXP vec) throw(std::range_error) : v()  {
> | 00028                 int i;
> | 00029                 if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
> | 00030                         throw std::range_error("DatetimeVector: invalid numeric vector in constructor");
> | 00031                 int len = Rf_length(vec);
> | 00032                 if (len == 0)
> | 00033                         throw std::range_error("DatetimeVector: null vector in constructor");
> | 00034                 v.resize(len);
> | 00035                 for (i = 0; i < len; i++)
> | 00036                         v[i] = Datetime( static_cast<double>(REAL(vec)[i]));
> | 00037         }
> | 
> | 
> | where line 36 calls Datetime without the ability to provide the format. Would it be possible for this to be changed to allow the format of the Datetime string. Currently I have to either convert the date string in R, or use an additional loop to convert the format, which make this slower than using the standard methods in R.
> | 
> | Sorry in advance if my understanding is incorrect.
> 
> I fear you may be confused.  To see what constructors are defined, see the
> corresponding class declaration in its header. For Datetime we get
> 
>    [...]
> 	      
>    class Datetime {
>    public:	
> 		Datetime();
> 		Datetime(SEXP s); 
> 		Datetime(const double &dt);	// from double, just like POSIXct
> 		Datetime(const std::string &s, const std::string &fmt="%Y-%m-%d %H:%M:%OS");
> 		Datetime(const Datetime &copy);
> 
>    [...]
> 
> implying that we can construct 'empty', from SEXP, from double, from string +
> string and lastly from an existing Datetime.
> 
> Also, your second code quote is 'an existance proof' of the asked-for
> constructor:  Had it now existed, the second quote would not have compiled.
> 
> As for your performance issues:  can you send a small mock-up example we can
> grind through benchmark() or other timing tools?
> 
> Dirk
> 
> -- 
> Gauss once played himself in a zero-sum game and won $50.
>                      -- #11 at http://www.gaussfacts.com



More information about the Rcpp-devel mailing list