Skip to content

Commit 40ae295

Browse files
committed
Added assignment operators to newDatetimeVector and newDateVector. Fixed a small bug in their copy construtors.
1 parent 45cbf37 commit 40ae295

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

inst/include/Rcpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#include <Rcpp/Formula.h>
5757
#include <Rcpp/DataFrame.h>
5858

59-
// #define RCPP_NEW_DATE_DATETIME_VECTORS 1
59+
#define RCPP_NEW_DATE_DATETIME_VECTORS 1
6060
#include <Rcpp/date_datetime/date_datetime.h>
6161

6262
#include <Rcpp/Na_Proxy.h>

inst/include/Rcpp/date_datetime/newDateVector.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ namespace Rcpp {
2929
class newDateVector : public NumericVector {
3030
public:
3131
template <int RTYPE, bool NA, typename VEC>
32-
newDateVector(const VectorBase<RTYPE,NA,VEC>& vec) : NumericVector(vec) {}
32+
newDateVector(const VectorBase<RTYPE,NA,VEC>& vec) : NumericVector(vec) {
33+
setClass();
34+
}
3335

3436
newDateVector(SEXP vec) : NumericVector(vec) { setClass(); }
3537
newDateVector(int n) : NumericVector(n) { setClass(); }
@@ -41,6 +43,14 @@ namespace Rcpp {
4143
v[i] = (*this)[i];
4244
return v;
4345
}
46+
47+
inline newDateVector &operator=(const newDateVector &rhs) {
48+
if (this != &rhs) {
49+
NumericVector::operator=(rhs);
50+
}
51+
52+
return *this;
53+
}
4454

4555
private:
4656

inst/include/Rcpp/date_datetime/newDatetimeVector.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace Rcpp {
3030
public:
3131
template <int RTYPE, bool NA, typename VEC>
3232
newDatetimeVector(const VectorBase<RTYPE,NA,VEC>& other) :
33-
NumericVector(other) {}
33+
NumericVector(other) {
34+
setClass(other.attr("tzone"));
35+
}
3436

3537
newDatetimeVector(SEXP vec, const char* tz = "") :
3638
NumericVector(vec) {
@@ -49,6 +51,15 @@ namespace Rcpp {
4951
v[i] = (*this)[i];
5052
return v;
5153
}
54+
55+
inline newDatetimeVector &operator=(const newDatetimeVector &rhs) {
56+
if (this != &rhs) {
57+
NumericVector::operator=(rhs);
58+
this->attr("tzone") = rhs.attr("tzone");
59+
}
60+
61+
return *this;
62+
}
5263

5364
private:
5465

inst/unitTests/cpp/dates.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,17 @@ SEXP DatetimeVector_ctor(DatetimeVector d) {
158158
DatetimeVector dt = DatetimeVector(d);
159159
return wrap(dt);
160160
}
161+
162+
// [[Rcpp::export]]
163+
DatetimeVector DatetimeVector_assignment(DatetimeVector v1,
164+
DatetimeVector v2) {
165+
166+
v1 = v2;
167+
return v1;
168+
}
169+
170+
// [[Rcpp::export]]
171+
DateVector DateVector_assignment(DateVector v1, DateVector v2) {
172+
v1 = v2;
173+
return v1;
174+
}

inst/unitTests/runit.Date.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,19 @@ if (.runThisTest) {
181181
checkEquals(fun(vec), c(now, rep(posixtNA, 3), now+2.345), msg = "Datetime.ctor.NA.NaN.Inf.set")
182182
}
183183
}
184+
185+
test.DatetimeVector.assignment <- function() {
186+
now <- Sys.time()
187+
v1 <- c(now, now + 1, now + 2)
188+
v2 <- c(now + 3, now + 4, now + 5)
189+
checkEquals(v2, DatetimeVector_assignment(v1, v2))
190+
}
191+
192+
test.DateVector.assignment <- function() {
193+
now <- Sys.Date()
194+
v1 <- c(now, now + 1, now + 2)
195+
v2 <- c(now + 3, now + 4, now + 5)
196+
checkEquals(v2, DateVector_assignment(v1, v2))
197+
}
184198

185199
}

0 commit comments

Comments
 (0)