[Rcpp-commits] r478 - in pkg: inst/unitTests src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jan 27 02:49:19 CET 2010
Author: edd
Date: 2010-01-27 02:49:18 +0100 (Wed, 27 Jan 2010)
New Revision: 478
Added:
pkg/inst/unitTests/runit.RcppDatetime.R
Modified:
pkg/src/RcppDatetime.cpp
pkg/src/RcppDatetime.h
Log:
new unit test for RcppDatetime
new SEXP-based constructor for RcppDatetime
bug fix for operator- [thanks to Unit tests!]
Added: pkg/inst/unitTests/runit.RcppDatetime.R
===================================================================
--- pkg/inst/unitTests/runit.RcppDatetime.R (rev 0)
+++ pkg/inst/unitTests/runit.RcppDatetime.R 2010-01-27 01:49:18 UTC (rev 478)
@@ -0,0 +1,74 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# Rcpp is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+.setUp <- function(){
+ suppressMessages( require( inline ) )
+}
+
+test.RcppDatetime.get.functions <- function() {
+ src <- '//RcppDatetime dt = RcppDatetime(946774923.123456); // 2001-02-03 01:02:03.123456
+ RcppDatetime dt = RcppDatetime(1152338523.456789); // as.POSIXct("2006-07-08 01:02:03.456789")
+ RcppResultSet rs;
+ //std::cout << dt << std::endl;
+ rs.add("year", dt.getYear());
+ rs.add("month", dt.getMonth());
+ rs.add("day", dt.getDay());
+ rs.add("wday", dt.getWeekday());
+ rs.add("hour", dt.getHour());
+ rs.add("minute", dt.getMinute());
+ rs.add("second", dt.getSecond());
+ rs.add("microsec", dt.getMicroSec());
+ //rs.add("dt", dt);
+ return rs.getReturnList();';
+ funx <- cfunction(signature(), src, Rcpp=TRUE)
+ checkEquals(funx(),
+ list(year=2006, month=7, day=8, wday=6,
+ hour=1, minute=2, second=3, microsec=456789),
+ msg = "RcppDate.get.functions")
+}
+
+test.RcppDatetime.operators <- function() {
+ src <- 'RcppDatetime d1 = RcppDatetime(946774923.123456);
+ //RcppDatetime d1 = RcppDatetime(1152338523.456789); // as.POSIXct("2006-07-08 01:02:03.456789")
+ RcppDatetime d2 = d1 + 60*60;
+ RcppResultSet rs;
+ rs.add("diff", d2 - d1);
+ rs.add("bigger", d2 > d1);
+ rs.add("smaller", d2 < d1);
+ rs.add("equal", d2 == d1);
+ rs.add("ge", d2 >= d1);
+ rs.add("le", d2 <= d1);
+ return rs.getReturnList();';
+ funx <- cfunction(signature(), src, Rcpp=TRUE)
+ checkEquals(funx(), list(diff=3600, bigger=1, smaller=0, equal=0, ge=1, le=0), msg = "RcppDatetime.operators")
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Modified: pkg/src/RcppDatetime.cpp
===================================================================
--- pkg/src/RcppDatetime.cpp 2010-01-26 21:58:37 UTC (rev 477)
+++ pkg/src/RcppDatetime.cpp 2010-01-27 01:49:18 UTC (rev 478)
@@ -31,6 +31,12 @@
m_us(0) {
};
+RcppDatetime::RcppDatetime(SEXP ds) {
+ m_d = REAL(ds)[0];
+ m_parsed = false;
+ m_us = 0;
+};
+
void RcppDatetime::parseTime() {
// tt is the nb of seconds, ignores fractional microsec
time_t tt = static_cast<time_t>(floor(m_d));
Modified: pkg/src/RcppDatetime.h
===================================================================
--- pkg/src/RcppDatetime.h 2010-01-26 21:58:37 UTC (rev 477)
+++ pkg/src/RcppDatetime.h 2010-01-27 01:49:18 UTC (rev 478)
@@ -27,11 +27,11 @@
class RcppDatetime {
private:
- double m_d;
- bool m_parsed;
- int m_us; // microseconds giving us fractional seconds
- struct tm m_tm;
- void parseTime();
+ double m_d; // fractional seconds since epoch as eg POSIXct
+ bool m_parsed; // has m_tm been set based on m_d ?
+ int m_us; // microseconds not in POSIX time structure
+ struct tm m_tm; // time structure as eg POSIXlt
+ void parseTime(); // sets m_tm and m_us based on m_d
protected:
friend class ColDatum;
@@ -39,6 +39,7 @@
public:
RcppDatetime(void);
RcppDatetime(const double d);
+ RcppDatetime(SEXP ds);
double getFractionalTimestamp(void) const { return m_d; }
int getYear(void) { if (!m_parsed) parseTime(); return m_tm.tm_year + 1900; }
@@ -50,7 +51,7 @@
int getSecond(void) { if (!m_parsed) parseTime(); return m_tm.tm_sec; }
int getMicroSec(void) { if (!m_parsed) parseTime(); return m_us; }
- friend double operator-(const RcppDatetime& dt1, const RcppDatetime& dt2) { return dt2.m_d - dt1.m_d; }
+ friend double operator-(const RcppDatetime& dt1, const RcppDatetime& dt2) { return dt1.m_d - dt2.m_d; }
friend bool operator<(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d < dt2.m_d; }
friend bool operator<=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d <= dt2.m_d; }
friend bool operator>(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d > dt2.m_d; }
More information about the Rcpp-commits
mailing list