[Rcpp-devel] Datetime and DatetimeVector definitions

Dirk Eddelbuettel edd at debian.org
Thu May 19 15:04:11 CEST 2011


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