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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 24 01:24:05 CEST 2010


Author: edd
Date: 2010-06-24 01:24:05 +0200 (Thu, 24 Jun 2010)
New Revision: 1696

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Date.h
   pkg/Rcpp/src/Date.cpp
Log:
completed Date class


Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-06-23 20:57:24 UTC (rev 1695)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-23 23:24:05 UTC (rev 1696)
@@ -1,6 +1,7 @@
 2010-06-23  Dirk Eddelbuettel  <edd at debian.org>
 
-	* src/Date.cpp: Import mktime00() from R's src/main/datetime.c
+	* src/Date.cpp: Import mktime00() from R's src/main/datetime.c,
+	  completed class implementation
 	* inst/include/Rcpp/Date.h: Add mktime00() declaration
 
 	* inst/include/Rcpp/DateVector.h: New DateVector class

Modified: pkg/Rcpp/inst/include/Rcpp/Date.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Date.h	2010-06-23 20:57:24 UTC (rev 1695)
+++ pkg/Rcpp/inst/include/Rcpp/Date.h	2010-06-23 23:24:05 UTC (rev 1696)
@@ -32,27 +32,24 @@
 		Date(const int &dt);	// from integer, just like R (with negative dates before Jan 1, 1970)
 		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();
+		Date(const Date &copy);
+		~Date() {};
 		
-		int getDate(void) const; 
+		int getDate(void) const { return d; } 
 
 		static const int QLtoJan1970Offset;  // Offset between R / Unix epoch date and the QL base date
 
-#if 0		
-		// rest to follow
-		// assignment
-		// copy
-		// ...
-		
+		Date & operator=(const Date &newdate); 		// copy assignment operator 
+
 		// Minimal set of date operations.
-		friend Date   operator+(const Date &date, int offset);
-		friend int    operator-(const Date& date1, const Date& date2);
-		friend bool   operator<(const Date &date1, const Date& date2);
-		friend bool   operator>(const Date &date1, const Date& date2);
-		friend bool   operator==(const Date &date1, const Date& date2);
-		friend bool   operator>=(const Date &date1, const Date& date2);
-		friend bool   operator<=(const Date &date1, const Date& date2);
-#endif
+		friend Date  operator+(const Date &date, int offset);
+		friend int   operator-(const Date& date1, const Date& date2);
+		friend bool  operator<(const Date &date1, const Date& date2);
+		friend bool  operator>(const Date &date1, const Date& date2);
+		friend bool  operator==(const Date &date1, const Date& date2);
+		friend bool  operator>=(const Date &date1, const Date& date2);
+		friend bool  operator<=(const Date &date1, const Date& date2);
+		friend bool  operator!=(const Date &date1, const Date& date2);
 
     private:
         int d;					// day number, relative to epoch of Jan 1, 1970

Modified: pkg/Rcpp/src/Date.cpp
===================================================================
--- pkg/Rcpp/src/Date.cpp	2010-06-23 20:57:24 UTC (rev 1695)
+++ pkg/Rcpp/src/Date.cpp	2010-06-23 23:24:05 UTC (rev 1696)
@@ -38,8 +38,7 @@
     }
 
     Date::Date(const std::string &s, const std::string &fmt) {
-	// we cheat and call strptime() from R
-	Rcpp::Function strptime("strptime");
+	Rcpp::Function strptime("strptime");	// we cheat and call strptime() from R
 	d = Rcpp::as<int>(strptime(s, fmt));
     }
 
@@ -48,7 +47,7 @@
 	struct tm tm;;
 	tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_isdst = 0;
 
-	// allow for special case (yyyy, mm, dd) which we prefer over (mm, dd, year)
+	// allow for ISO-notation case (yyyy, mm, dd) which we prefer over (mm, dd, year)
 	if (mon >= 1900 && day <= 12 && year <= 31) {
 	    tm.tm_year = mon - 1900;
 	    tm.tm_mon  = day - 1;       // range 0 to 11
@@ -58,25 +57,26 @@
 	    tm.tm_mon   = mon - 1;	// range 0 to 11
 	    tm.tm_year  = year - 1900;
 	}
-	double tmp = mktime00(tm); // use R's internal mktime() replacement
+	double tmp = mktime00(tm); 	// use mktime() replacement borrowed from R
 	d = tmp/(24*60*60);
     }
 
-    Date::~Date() {
-	// nothing to do
+    Date::Date(const Date &copy) {
+	d = copy.d;
     }
 
-    int Date::getDate(void) const { 
-	return d; 
-    };
+    Date & Date::operator=(const Date & newdate) {
+	if (this != &newdate) {
+	    d = newdate.d;
+	}
+	return *this;
+    }
 
-
     // Taken from R's src/main/datetime.c and made a member function called with C++ reference
     /* Substitute for mktime -- no checking, always in GMT */
     double Date::mktime00(struct tm &tm) const {
 
-	static const int days_in_month[12] =
-	    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+	static const int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
         #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
         #define days_in_year(year) (isleap(year) ? 366 : 365)
@@ -115,7 +115,20 @@
 	    + (day + excess * 730485) * 86400.0;
     }
 
+    Date operator+(const Date &date, int offset) {
+	Date newdate(date.d);
+	newdate.d += offset;
+	return newdate;
+    }
 
+    int   operator-(const Date& d1, const Date& d2) { return d2.d - d1.d; }
+    bool  operator<(const Date &d1, const Date& d2) { return d1.d < d2.d; }
+    bool  operator>(const Date &d1, const Date& d2) { return d1.d > d2.d; }
+    bool  operator==(const Date &d1, const Date& d2) { return d1.d == d2.d; };
+    bool  operator>=(const Date &d1, const Date& d2) { return d1.d >= d2.d; };
+    bool  operator<=(const Date &d1, const Date& d2) { return d1.d <= d2.d; };
+    bool  operator!=(const Date &d1, const Date& d2) { return d1.d != d2.d; };
+
     template <> SEXP wrap(const Date &date) {
 	SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
 	REAL(value)[0] = date.getDate();



More information about the Rcpp-commits mailing list