Skip to content

Add DateVector and DatetimeVector default ctors (closes #1384) #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2025-05-23 Dirk Eddelbuettel <[email protected]>

* inst/include/Rcpp/date_datetime/newDateVector.h: Add default
constructor
* inst/include/Rcpp/date_datetime/newDatetimeVector.h: Idem

* inst/tinytest/test_date.R: Add tests
* inst/tinytest/cpp/dates.cpp: Idem

2025-05-06 Dirk Eddelbuettel <[email protected]>

* DESCRIPTION (Version, Date): Roll micro version and date
Expand Down
6 changes: 5 additions & 1 deletion inst/include/Rcpp/date_datetime/newDateVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// newDateVector.h: Rcpp R/C++ interface class library -- Date vector support
//
// Copyright (C) 2016 Dirk Eddelbuettel
// Copyright (C) 2016 - 2025 Dirk Eddelbuettel
//
// This file is part of Rcpp.
//
Expand All @@ -28,6 +28,10 @@ namespace Rcpp {

class newDateVector : public NumericVector {
public:
newDateVector() : NumericVector() {
setClass();
}

template <int RTYPE, bool NA, typename VEC>
newDateVector(const VectorBase<RTYPE,NA,VEC>& vec) : NumericVector(vec) {
setClass();
Expand Down
6 changes: 5 additions & 1 deletion inst/include/Rcpp/date_datetime/newDatetimeVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ namespace Rcpp {

class newDatetimeVector : public NumericVector {
public:
newDatetimeVector(const char* tz = "") : NumericVector() {
setClass(tz);
}

template <int RTYPE, bool NA, typename VEC>
newDatetimeVector(const VectorBase<RTYPE,NA,VEC>& other, const char* tz = "") :
NumericVector(other) {
setClass(tz);
setClass(tz);
}

newDatetimeVector(SEXP vec, const char* tz = "") :
Expand Down
13 changes: 10 additions & 3 deletions inst/tinytest/cpp/dates.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//

// dates.cpp: Rcpp R/C++ interface class library -- Date + Datetime tests
//
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
Expand Down Expand Up @@ -245,4 +244,12 @@ bool has_na_dtv(const Rcpp::DatetimeVector d) {
return Rcpp::is_true(Rcpp::any(Rcpp::is_na(d)));
}

// [[Rcpp::export]]
Rcpp::DateVector default_ctor_datevector() {
return Rcpp::DateVector();
}

// [[Rcpp::export]]
Rcpp::DatetimeVector default_ctor_datetimevector() {
return Rcpp::DatetimeVector();
}
14 changes: 13 additions & 1 deletion inst/tinytest/test_date.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## Copyright (C) 2010 - 2019 Dirk Eddelbuettel and Romain Francois
## Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
##
## This file is part of Rcpp.
##
Expand Down Expand Up @@ -241,3 +241,15 @@ dvt <- Sys.time() + 0:2
expect_true(has_na_dtv(dvt) == FALSE, info="DatetimeVector.NAtest.withoutNA")
dvt[1] <- NA
expect_true(has_na_dtv(dvt) == TRUE, info="DatetimeVector.NAtest.withNA")

## default ctor: date
dv <- default_ctor_datevector()
expect_true(inherits(dv, "Date"))
expect_equal(length(dv), 0L)
expect_equal(dv, as.Date(double(), origin="1970-01-01")) # origin for R < 4.3.0

## default ctor: datetime
dtv <- default_ctor_datetimevector()
expect_true(inherits(dtv, "POSIXct"))
expect_equal(length(dtv), 0L)
expect_equal(dtv, as.POSIXct(double(), origin="1970-01-01")) # origin for R < 4.3.0