[Rcpp-commits] r3790 - in pkg/Rcpp: . inst inst/include/Rcpp src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 6 18:49:46 CEST 2012


Author: edd
Date: 2012-10-06 18:49:46 +0200 (Sat, 06 Oct 2012)
New Revision: 3790

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/DESCRIPTION
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/inst/include/Rcpp/Date.h
   pkg/Rcpp/src/Date.cpp
   pkg/Rcpp/src/DateVector.cpp
Log:
Dates now cope with NA, NaN and Inf representation, and are internally stored as fractional days since the epoch


Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-10-06 03:44:18 UTC (rev 3789)
+++ pkg/Rcpp/ChangeLog	2012-10-06 16:49:46 UTC (rev 3790)
@@ -1,3 +1,14 @@
+2012-10-06  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/include/Rcpp/Date.h (Rcpp): Dates are now represented as
+	doubles, also added accessor functions for double and retained int
+
+	* src/Date.cpp (Rcpp): Convert from SEXP to double, added constructor
+	from double and added checks for 'finite' representation before
+	converting to 'struct tm' adding support for NA, NaN and Inf
+
+	* src/DateVector.cpp: Instantiate Date types as double
+
 2012-10-05  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/exceptions.cpp: Applied patch by Martin Morgan which untangles
@@ -2,3 +13,3 @@
 	the clang includes driven by version, and uses direct tests for
-	presence of the header file 
+	presence of the header file
 	* inst/include/RcppCommon.h: No longer define CLANG version meta

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2012-10-06 03:44:18 UTC (rev 3789)
+++ pkg/Rcpp/DESCRIPTION	2012-10-06 16:49:46 UTC (rev 3790)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Seamless R and C++ Integration
-Version: 0.9.14.1
+Version: 0.9.14.2
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Douglas Bates and John Chambers

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2012-10-06 03:44:18 UTC (rev 3789)
+++ pkg/Rcpp/inst/NEWS.Rd	2012-10-06 16:49:46 UTC (rev 3790)
@@ -7,7 +7,9 @@
     \item Untangling the clang++ build issue about the location of the
     exceptions header by directly checking for the include file -- an
     approach provided by Martin Morgan in a kindly contributed patch
-    as unit tests for them. 
+    as unit tests for them.
+    \item The \code{Date} type now correctly handles \code{NA},
+    \code{NaN} and \code{Inf} representation
   }
 }
 

Modified: pkg/Rcpp/inst/include/Rcpp/Date.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Date.h	2012-10-06 03:44:18 UTC (rev 3789)
+++ pkg/Rcpp/inst/include/Rcpp/Date.h	2012-10-06 16:49:46 UTC (rev 3790)
@@ -2,7 +2,7 @@
 //
 // Date.h: Rcpp R/C++ interface class library -- dates
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012  Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -30,13 +30,14 @@
     public:	
 		Date();
 		Date(SEXP s); 
-		Date(const int &dt);	// from integer, just like R (with negative dates before Jan 1, 1970)
+		Date(const int &dt);		// from integer (with negative dates before Jan 1, 1970)
+		Date(const double &dt);		// from fractional integer since epoch, just like R 
 		Date(const std::string &s, const std::string &fmt="%Y-%m-%d");
 		Date(const unsigned int &m, const unsigned int &d, const unsigned int &y); 
 		Date(const Date &copy);
 		~Date() {};
 		
-		int getDate(void) const { return m_d; } 
+		double getDate(void) const { return m_d; } 
 
 		// intra-day useless for date class
 		//int getSeconds() const { return m_tm.tm_sec; }
@@ -64,7 +65,7 @@
 		friend bool  operator!=(const Date &date1, const Date& date2);
 
     private:
-        int m_d;					// day number, relative to epoch of Jan 1, 1970
+        double m_d;					// (fractional) day number, relative to epoch of Jan 1, 1970
         struct tm m_tm;				// standard time representation
 
 		void update_tm();			// update m_tm based on m_d

Modified: pkg/Rcpp/src/Date.cpp
===================================================================
--- pkg/Rcpp/src/Date.cpp	2012-10-06 03:44:18 UTC (rev 3789)
+++ pkg/Rcpp/src/Date.cpp	2012-10-06 16:49:46 UTC (rev 3790)
@@ -2,7 +2,7 @@
 //
 // Date.h: Rcpp R/C++ interface class library -- Date type
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2014  Dirk Eddelbuettel and Romain Francois
 //
 //    The mktime00() as well as the gmtime_() replacement function are
 //    Copyright (C) 2000 - 2010  The R Development Core Team.
@@ -43,7 +43,7 @@
     }
 
     Date::Date(SEXP d) {
-	m_d = Rcpp::as<int>(d); 
+	m_d = Rcpp::as<double>(d);
 	update_tm();
     }
 
@@ -52,6 +52,11 @@
 	update_tm();
     }
 
+    Date::Date(const double &dt) {
+        m_d = dt;
+        update_tm();
+    }
+
     Date::Date(const std::string &s, const std::string &fmt) {
 	Rcpp::Function strptime("strptime");	// we cheat and call strptime() from R
 	Rcpp::Function asDate("as.Date");	// and we need to convert to Date
@@ -92,9 +97,13 @@
     }
 
     void Date::update_tm() {
-	time_t t = 24*60*60 * m_d;	// days since epoch to seconds since epoch
-	//m_tm = *gmtime(&t);		// this may need a Windows fix, re-check R's datetime.c
-	m_tm = *gmtime_(&t);
+        if (R_FINITE(m_d)) {
+            time_t t = 24*60*60 * m_d;		// (fractional) days since epoch to seconds since epoch
+            m_tm = *gmtime_(&t);
+        } else {
+            m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
+            m_tm.tm_min = m_tm.tm_hour = m_tm.tm_mday = m_tm.tm_mon  = m_tm.tm_year = NA_INTEGER;
+        }
     }
 
     // Taken from R's src/main/datetime.c and made a member function called with C++ reference

Modified: pkg/Rcpp/src/DateVector.cpp
===================================================================
--- pkg/Rcpp/src/DateVector.cpp	2012-10-06 03:44:18 UTC (rev 3789)
+++ pkg/Rcpp/src/DateVector.cpp	2012-10-06 16:49:46 UTC (rev 3790)
@@ -2,7 +2,7 @@
 //
 // DateVector.cpp: Rcpp R/C++ interface class library -- Date vector support
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012  Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -33,7 +33,7 @@
             throw std::range_error("DateVector: null vector in constructor");
         v.resize(len);
         for (i = 0; i < len; i++)
-            v[i] = Date( static_cast<int>(REAL(vec)[i]));
+            v[i] = Date( static_cast<double>(REAL(vec)[i]));
     }
 
 



More information about the Rcpp-commits mailing list