r-devel, I've been trying to write a C plugin for R, and I'm struggling to understand how dates are represented. I can create a date in R:> mydate<-as.Date(1, origin="1900-01-01") mydate[1] "1900-01-02" When I pass my date to a plugin, though, it's type is that of a real. There doesn't seem to be a date type in Rinternals.h, is there a way to recognize that a value is a date rather than a real? Equally, does anyone know if it's possible to create date values in the C api? Thanks, Phil Lee Philip Lee Morgan Stanley | Technology 750 Seventh Avenue, 12th Floor | New York, NY 10019 Phil.Lee at morganstanley.com -------------------------------------------------------- NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error.
Dates are number of days since 1970-01-01 that is it. to get data of Date in C: library(inline) code = 'Rprintf("%f", REAL(x)[0]); return R_NilValue;' f = cfunction(signature(x="double"), body=code) na = f(Sys.date()) to create an object of class Date: --- in C --- SEXP res = allocVector(REALSXP, n); return res; // one can assign class here, but one can also do it on return to R --- in R --- res = .Call("myDateFun", n) class(res) = "Date" Best, Oleg Lee, Philip (IT) wrote:> r-devel, > > I've been trying to write a C plugin for R, and I'm struggling to > understand how dates are represented. > > I can create a date in R: >> mydate<-as.Date(1, origin="1900-01-01") mydate > [1] "1900-01-02" > > When I pass my date to a plugin, though, it's type is that of a real. > There doesn't seem to be a date type in Rinternals.h, is there a way to > recognize that a value is a date rather than a real? > > Equally, does anyone know if it's possible to create date values in the > C api? > > Thanks, > Phil Lee > > Philip Lee > Morgan Stanley | Technology > 750 Seventh Avenue, 12th Floor | New York, NY 10019 > Phil.Lee at morganstanley.com > -------------------------------------------------------- > > NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466
On 9/17/2008 1:24 PM, Lee, Philip (IT) wrote:> r-devel, > > I've been trying to write a C plugin for R, and I'm struggling to > understand how dates are represented. > > I can create a date in R: >> mydate<-as.Date(1, origin="1900-01-01") mydate > [1] "1900-01-02"See ?Date for the internal represention: it's a count of days from a particular epoch.> When I pass my date to a plugin, though, it's type is that of a real. > There doesn't seem to be a date type in Rinternals.h, is there a way to > recognize that a value is a date rather than a real?Look at the class attribute.> > Equally, does anyone know if it's possible to create date values in the > C api?Create a vector of doubles, and attach the right class to it. See section 5.8.5 of the R extensions manual for an example. Duncan Murdoch> > Thanks, > Phil Lee > > Philip Lee > Morgan Stanley | Technology > 750 Seventh Avenue, 12th Floor | New York, NY 10019 > Phil.Lee at morganstanley.com > -------------------------------------------------------- > > NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
If you are looking for examples of handling dates/times (specifically POSIXct) within C code, the dev branch of xts has quite a bit of code now. http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/dev/pkg/src/?root=xts Assigning the class (or any attribute) within the C code will be much faster than doing it outside of C, at least in my experience with larger data. setAttrib(ans, R_ClassSymbol, mkString("Date")) HTH Jeff On Wed, Sep 17, 2008 at 12:24 PM, Lee, Philip (IT) <Phil.Lee at morganstanley.com> wrote:> r-devel, > > I've been trying to write a C plugin for R, and I'm struggling to > understand how dates are represented. > > I can create a date in R: >> mydate<-as.Date(1, origin="1900-01-01") mydate > [1] "1900-01-02" > > When I pass my date to a plugin, though, it's type is that of a real. > There doesn't seem to be a date type in Rinternals.h, is there a way to > recognize that a value is a date rather than a real? > > Equally, does anyone know if it's possible to create date values in the > C api? > > Thanks, > Phil Lee > > Philip Lee > Morgan Stanley | Technology > 750 Seventh Avenue, 12th Floor | New York, NY 10019 > Phil.Lee at morganstanley.com > -------------------------------------------------------- > > NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Jeffrey Ryan jeffrey.ryan at insightalgo.com ia: insight algorithmics www.insightalgo.com