||Thinking inside the box|
Tue, 18 Jan 2011
RcppBDT stands for Rcpp Boost Date_Time. It employs what we call Rcpp modules: a mechanism which provides easier ways to expose C++ functions and classes to R (and which bears some resemblance to Boost.Python---see this vignette about Rcpp modules for more details). And thus RcppBDT provides R users with access to (some) Boost Date.Time functionality.
I used Boost Date.Time because
it is widely known, useful and well tested. It can also be used in pure
template mode not requiring linking (if one foregoes string parsing and
formatting which is fine here as we get this from R already; it can be added
Basic usage follows Rcpp modules and provides the C++ class via a Reference
Class in R. That means using a
Not very exiting yet: we create a date, using explicit year, months and date arguments and then format it. Something more useful follows:R> library(RcppBDT) Creating a new generic function for "print" in "RcppBDT" Creating a new generic function for "format" in "RcppBDT" R> d <- new(bdtMod$date, 2011, 1, 18) R> format(d)  "2011-01-18" R>
We can use accessors to extracts parts of the date, or use functions to convert the date to different representations such as a modified Julian date. Moreover, given a date, we can apply helper functions such as get me the date of the beginning of the next month.R> d$getDayOfWeek()  2 R> d$getModJulian()  55579 R> d$getFirstOfNextMonth()  "2011-02-01" R>
More interesting still are helper functions such as the ones below. Note that this also shows the alternate access method using wrapper functions I added for the package, this may be more familiar to most R users:
This uses some of the constantsR> getNthDayOfWeek(third, Wed, Dec, 2010)  "2010-12-15" R> getLastDayOfWeekInMonth(Sat, Dec, 2010)  "2010-12-25" R> getFirstDayOfWeekInMonth(Sat, Dec, 2010)  "2010-12-04" R> getFirstDayOfWeekAfter(Wed, as.Date("2010-12-31"))  "2011-01-05" R>
As always, feedback would be welcome, both on the usefulness (or lack thereof) of the Boost Date.Time functionality as well as on the Rcpp modules wrapping. Additional Boost Date.Time functionality (durations, times, ...) may be added; contributions would be welcome. The rcpp-devel mailing list off the R-Forge page for Rcpp is the best place to start a discussion.