Improved kdb+/R conversion for datetime

181 views
Skip to first unread message

Dirk Eddelbuettel

unread,
Feb 3, 2009, 10:45:21 PM2/3/09
to personal...@googlegroups.com

I have started to experiment with kdb+ and q. As my preferred tool for analysis
is R, I started to work with the files in

https://code.kx.com/trac/browser/kx/kdb%2B/interfaces/r

which I turned into a (local) CRAN-style R package. I noticed one clear
shortcoming of this otherwise rather nice kdb+/R interface: datetime objects
got dropped to int, were only handled one-at-a-time rather than vectorised
and lead to segfaults due to what looks like a leftover reference count
decrementor r0(x).

The version below works on vectors as well as scalars, and converts to floats
with fractional seconds since the Unix epoch along with POSIXt / POSIXct
class attributes just like other R Datetime objects. [ The same can be done
for dates, only convert to INTSXP and set only one class attribute, "Date". ]

Hope this helps, Dirk


static SEXP from_datetime_kobject(K x)
{
SEXP result;
int i, length = x->n;
if (scalar(x)) {
result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = (kF(x)[0] + 10957) * 86400;
} else {
result = PROTECT(allocVector(REALSXP, length));
for(i = 0; i < length; i++) {
REAL(result)[i] = (kF(x)[i] + 10957) * 86400;
}
}
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(2);
return result;
}


--
Three out of two people have difficulties with fractions.

Chris Burke

unread,
Feb 12, 2009, 6:55:19 AM2/12/09
to Kdb+ Personal Developers
Thanks. I updated the date and datetime functions as suggested.
Reply all
Reply to author
Forward
0 new messages