[Rcpp-commits] r3746 - in pkg/RcppBDT: . src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 28 03:18:39 CEST 2012


Author: edd
Date: 2012-08-28 03:18:38 +0200 (Tue, 28 Aug 2012)
New Revision: 3746

Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/src/RcppBDTpt.cpp
Log:
better posix time to Datetime conversion


Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-08-27 03:00:18 UTC (rev 3745)
+++ pkg/RcppBDT/ChangeLog	2012-08-28 01:18:38 UTC (rev 3746)
@@ -1,3 +1,8 @@
+2012-08-27  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/RcppBDTpt.cpp: getDatetime() now uses struct tm and mktime
+	which dels correctly with offsets to UTC
+
 2012-08-26  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RcppBDTpt.cpp: Started module 'bdtPt' with posix time objects

Modified: pkg/RcppBDT/src/RcppBDTpt.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTpt.cpp	2012-08-27 03:00:18 UTC (rev 3745)
+++ pkg/RcppBDT/src/RcppBDTpt.cpp	2012-08-28 01:18:38 UTC (rev 3746)
@@ -44,10 +44,17 @@
         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;
-        return Rcpp::Datetime( x.total_seconds() + 1.0 * x.fractional_seconds() / x.ticks_per_second() );
+        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:



More information about the Rcpp-commits mailing list