The RcppBDT package interfaces
Boost.Date_Time with R. Both systems
have their own date representations—and this provides a nice
example of custom as<>() and wrap() converters. Here, we show a simplified example.
We start with the forward declarations:
Given these forward declarations, we can now define the converters.
For as(), we first instantiate a date object and use it to obtain
the year, month and day fields to create a boost::gregorian date.
Similarly, for the inverse operation, we construct an Rcpp date
from these components.
With these converters, we can now use a Boost Date_Time function.
As a simple example, we use the compute the first given weekday
after a date function.
We can use this to, say, find the first Monday after New Year in 2020:
[1] "2020-01-06"
We can also write a more concise form, knowing that compileAttributes() will insert
the call to as<>() as needed.
We still need a call to wrap() as the return-type of the function is Rcpp::Date()
whereas we defined our wrap() converter with a return-type of SEXP.