[Rcpp-commits] r3755 - in pkg/RcppBDT: . inst inst/include src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Sep 1 23:32:59 CEST 2012
Author: edd
Date: 2012-09-01 23:32:58 +0200 (Sat, 01 Sep 2012)
New Revision: 3755
Modified:
pkg/RcppBDT/ChangeLog
pkg/RcppBDT/inst/NEWS.Rd
pkg/RcppBDT/inst/include/RcppBDT.h
pkg/RcppBDT/src/RcppBDTpt.cpp
Log:
new template converters as and wrap for ptime
Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog 2012-09-01 20:42:15 UTC (rev 3754)
+++ pkg/RcppBDT/ChangeLog 2012-09-01 21:32:58 UTC (rev 3755)
@@ -1,3 +1,12 @@
+2012-09-01 Dirk Eddelbuettel <edd at debian.org>
+
+ * inst/include/RcppBDT.h: Added declarations for two new templated
+ converters as<>() and wrap() for the boost::posix_time::ptime class
+ * src/RcppBDTpt.cpp (Rcpp): Added implementation for new templated
+ converters as<>(), wrap() for the boost::posix_time::ptime class;
+ simplified existing code by using them; and added new setter from the
+ R Datetime class as well
+
2012-08-27 Dirk Eddelbuettel <edd at debian.org>
* src/RcppBDTpt.cpp: getDatetime() now uses struct tm and mktime
Modified: pkg/RcppBDT/inst/NEWS.Rd
===================================================================
--- pkg/RcppBDT/inst/NEWS.Rd 2012-09-01 20:42:15 UTC (rev 3754)
+++ pkg/RcppBDT/inst/NEWS.Rd 2012-09-01 21:32:58 UTC (rev 3755)
@@ -2,10 +2,13 @@
\title{News for Package \pkg{RcppBDT}}
\newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
-\section{Changes in version 0.x.y (2012-xx-yy)}{
+\section{Changes in version 0.2.2 (2012-09-01)}{
\itemize{
- \item New module providing time zone information such as offset to
+ \item New module 'bdtTz' providing time zone information such as offset to
UTC, amount of DST, abbreviated and full timezone names.
+ \item New module 'bdtDu' using 'posix_time::duration' for time durations types
+ \item New module 'bdtPt' using 'posix_time::ptime' for posix time,
+ down to nanosecond granularity (where hardware and OS permit it)
}
}
\section{Changes in version 0.2.1 (2012-08-08)}{
Modified: pkg/RcppBDT/inst/include/RcppBDT.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDT.h 2012-09-01 20:42:15 UTC (rev 3754)
+++ pkg/RcppBDT/inst/include/RcppBDT.h 2012-09-01 21:32:58 UTC (rev 3755)
@@ -39,19 +39,32 @@
#define RcppBDT_UseWithStrings 0
#if RcppBDT_UseWithString
- #include <boost/date_time/gregorian/gregorian.hpp> // Gregorian calendar types, including I/O
+ #include <boost/date_time/gregorian/gregorian.hpp> // Gregorian calendar types, with I/O
#else
#include <boost/date_time/gregorian/gregorian_types.hpp> // Gregorian calendar types, no I/O
#endif
#include <boost/date_time/local_time/local_time.hpp>
+
namespace Rcpp {
+
+ // First the 'date' class boost::gregorian::date
+ //
// non-intrusive extension via template specialisation
- template <> boost::gregorian::date as( SEXP dt );
+ template <> boost::gregorian::date as(SEXP dt);
+ //
+ // non-intrusive extension via template specialisation
+ template <> SEXP wrap(const boost::gregorian::date &d);
+
+ // Second the 'datetime' class boost::posix_time::ptime
+ //
// non-intrusive extension via template specialisation
- template <> SEXP wrap(const boost::gregorian::date &d);
+ template <> boost::posix_time::ptime as(SEXP dt);
+ //
+ // non-intrusive extension via template specialisation
+ template <> SEXP wrap(const boost::posix_time::ptime &dt);
}
#include <Rcpp.h>
Modified: pkg/RcppBDT/src/RcppBDTpt.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTpt.cpp 2012-09-01 20:42:15 UTC (rev 3754)
+++ pkg/RcppBDT/src/RcppBDTpt.cpp 2012-09-01 21:32:58 UTC (rev 3755)
@@ -21,6 +21,23 @@
#include <RcppBDT.h>
+// define template specialisations for as and wrap
+namespace Rcpp {
+ template <> boost::posix_time::ptime as( SEXP dtsexp ) {
+ Rcpp::Datetime dt(dtsexp);
+ boost::posix_time::ptime pt(boost::gregorian::date(dt.getYear(), dt.getMonth(), dt.getDay()),
+ boost::posix_time::time_duration(dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMicroSeconds()/1000.0));
+ return pt;
+ }
+
+ template <> SEXP wrap(const boost::posix_time::ptime &dt) {
+ boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1)); // TODO: cache this value...
+ boost::posix_time::time_duration x = dt - epoch; // this needs a UTC to local correction, but we get the fractional seconds
+ struct tm t = boost::posix_time::to_tm(dt); // this helps with UTC conversion
+ return Rcpp::wrap(Rcpp::Datetime( mktime(&t) + 1.0 * x.fractional_seconds() / x.ticks_per_second()));
+ }
+}
+
class bdtPt {
public:
@@ -37,22 +54,14 @@
void setFromTimeT(const time_t t) { m_pt = boost::posix_time::from_time_t(t); }
+ void setFromDatetime(const SEXP dt) { m_pt = Rcpp::as<boost::posix_time::ptime>(dt); }
+
+ Rcpp::Datetime getDatetime() { return Rcpp::wrap(m_pt); }
+
Rcpp::Date getDate() {
boost::gregorian::date::ymd_type ymd = m_pt.date().year_month_day(); // convert to date and then to y/m/d struct
return Rcpp::Date( ymd.year, ymd.month, ymd.day );
}
- // Rcpp::Datetime getDatetime() {
- // //std::cout << "At C++ level: " << m_pt << std::endl;
- // boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
- // boost::posix_time::time_duration x = m_pt - epoch; // needs a UTC to local correction
- // return Rcpp::Datetime( x.total_seconds() + 1.0 * x.fractional_seconds() / x.ticks_per_second() );// off by UTC difference
- // }
- Rcpp::Datetime getDatetime() {
- boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
- boost::posix_time::time_duration x = m_pt - epoch; // this needs a UTC to local correction, but we get the fractional seconds
- struct tm t = boost::posix_time::to_tm(m_pt); // this helps with UTC conversion
- return Rcpp::Datetime( mktime(&t) + 1.0 * x.fractional_seconds() / x.ticks_per_second());
- }
private:
boost::posix_time::ptime m_pt; // private ptime instace
@@ -72,7 +81,9 @@
.method("setFromTimeT", &bdtPt::setFromTimeT, "set from POSIXTct")
+ .method("setFromDatetime", &bdtPt::setFromDatetime, "set from Datetime representation")
+
+ .method("getDatetime", &bdtPt::getDatetime, "get datetime representation")
.method("getDate", &bdtPt::getDate, "get date representation")
- .method("getDatetime", &bdtPt::getDatetime, "get datetime representation")
;
}
More information about the Rcpp-commits
mailing list