Skip to content

Commit 40dde2f

Browse files
authored
Merge pull request #557 from RcppCore/feature/new_date_vector
Feature/new date and datetime vector classes (closes #34)
2 parents 6f81b46 + 90f9487 commit 40dde2f

File tree

11 files changed

+215
-15
lines changed

11 files changed

+215
-15
lines changed

ChangeLog

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
2016-10-18 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/include/Rcpp/date_datetime/Date.h (Rcpp): Add operator double()
4+
* inst/include/Rcpp/date_datetime/Datetime.h (Rcpp): Ditto
5+
6+
2016-10-17 Dirk Eddelbuettel <[email protected]>
7+
8+
* inst/include/Rcpp/date_datetime/newDatetimeVector.h (Rcpp): Allow
9+
setting of timezone attribute; default not-set leads to local as in R
10+
11+
2016-10-16 Dirk Eddelbuettel <[email protected]>
12+
13+
* inst/include/Rcpp.h (RCPP_NEW_DATE_DATETIME_VECTORS): Add a new
14+
compile-time #define to switch between 'old' (existing) and 'new'
15+
DateVector and DatimeVector classes. Currently uses 'new' for testing
16+
plan to switch to proper deprecation schedule.
17+
18+
* inst/include/Rcpp/date_datetime/date_datetime.h: New header file (and
19+
directory) regrouping all Date and Datetime headers, scalar and vector
20+
21+
* inst/include/Rcpp/date_datetime/Date.h: Moved one directory down
22+
* inst/include/Rcpp/date_datetime/Datetime.h: Ditto
23+
* inst/include/Rcpp/date_datetime/oldDateVector.h: Moved and renamed
24+
* inst/include/Rcpp/date_datetime/oldDatetimeVector.h: Ditto
25+
26+
* inst/include/Rcpp/date_datetime/newDateVector.h: New implementation
27+
inheriting from NumericVector, still optional
28+
* inst/include/Rcpp/date_datetime/newDatetimeVector.h: Ditto
29+
130
2016-09-05 Dirk Eddelbuettel <[email protected]>
231

332
* DESCRIPTION (Version): Roll minor version

DESCRIPTION

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 0.12.7.1
4-
Date: 2016-09-05
5-
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey,
6-
Qiang Kou, Douglas Bates and John Chambers
3+
Version: 0.12.7.2
4+
Date: 2016-10-18
5+
Author: Dirk Eddelbuettel, Romain Francois [2009-2013], JJ Allaire, Kevin Ushey,
6+
Qiang Kou, Douglas Bates [2010-2012] and John Chambers
77
Maintainer: Dirk Eddelbuettel <[email protected]>
88
Description: The 'Rcpp' package provides R functions as well as C++ classes which
99
offer a seamless integration of R and C++. Many R data types and objects can be

inst/include/Rcpp.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@
5555
#include <Rcpp/StringTransformer.h>
5656
#include <Rcpp/Formula.h>
5757
#include <Rcpp/DataFrame.h>
58-
#include <Rcpp/Date.h>
59-
#include <Rcpp/DateVector.h>
60-
#include <Rcpp/Datetime.h>
61-
#include <Rcpp/DatetimeVector.h>
58+
59+
// #define RCPP_NEW_DATE_DATETIME_VECTORS 1
60+
#include <Rcpp/date_datetime/date_datetime.h>
6261

6362
#include <Rcpp/Na_Proxy.h>
6463

inst/include/Rcpp/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define RCPP_VERSION Rcpp_Version(0,12,7)
3131

3232
// the current source snapshot
33-
#define RCPP_DEV_VERSION RcppDevVersion(0,12,7,0)
33+
#define RCPP_DEV_VERSION RcppDevVersion(0,12,7,1)
3434

3535
#endif
3636

inst/include/Rcpp/Date.h renamed to inst/include/Rcpp/date_datetime/Date.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ namespace Rcpp {
9898
return traits::is_na<REALSXP>(m_d);
9999
}
100100

101+
operator double() const {
102+
return m_d;
103+
}
104+
101105
private:
102106
double m_d; // (fractional) day number, relative to epoch of Jan 1, 1970
103107
struct tm m_tm; // standard time representation

inst/include/Rcpp/Datetime.h renamed to inst/include/Rcpp/date_datetime/Datetime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ namespace Rcpp {
6565

6666
inline int is_na() const { return traits::is_na<REALSXP>(m_dt); }
6767

68+
operator double() const { return m_dt; }
69+
6870
private:
6971
double m_dt; // fractional seconds since epoch
7072
struct tm m_tm; // standard time representation
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
//
3+
// date_datetime.h: Rcpp R/C++ interface class library -- Date and Datetime support
4+
//
5+
// Copyright (C) 2016 Dirk Eddelbuettel
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#ifndef Rcpp__Date_Datetime_h
23+
#define Rcpp__Date_Datetime_h
24+
25+
#include <Rcpp/date_datetime/Date.h>
26+
#include <Rcpp/date_datetime/oldDateVector.h>
27+
#include <Rcpp/date_datetime/newDateVector.h>
28+
29+
#include <Rcpp/date_datetime/Datetime.h>
30+
#include <Rcpp/date_datetime/oldDatetimeVector.h>
31+
#include <Rcpp/date_datetime/newDatetimeVector.h>
32+
33+
namespace Rcpp {
34+
35+
// this will not be on by default
36+
#if defined(RCPP_NEW_DATE_DATETIME_VECTORS)
37+
38+
typedef newDateVector DateVector;
39+
typedef newDatetimeVector DatetimeVector;
40+
41+
#else
42+
43+
// for now the fallback-default is to use the existing classes
44+
typedef oldDateVector DateVector;
45+
typedef oldDatetimeVector DatetimeVector;
46+
47+
#endif
48+
49+
}
50+
51+
#endif
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
//
3+
// newDateVector.h: Rcpp R/C++ interface class library -- Date vector support
4+
//
5+
// Copyright (C) 2016 Dirk Eddelbuettel
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#ifndef Rcpp__newDateVector_h
23+
#define Rcpp__newDateVector_h
24+
25+
#include <RcppCommon.h>
26+
27+
namespace Rcpp {
28+
29+
class newDateVector : public NumericVector {
30+
public:
31+
newDateVector(SEXP vec) : NumericVector(vec) { setClass(); }
32+
newDateVector(int n) : NumericVector(n) { setClass(); }
33+
34+
inline std::vector<Date> getDates() const {
35+
int n = this->size();
36+
std::vector<Date> v(n);
37+
for (int i=0; i<n; i++)
38+
v[i] = (*this)[i];
39+
return v;
40+
}
41+
42+
private:
43+
44+
void setClass() {
45+
Shield<SEXP> dateclass(Rf_mkString("Date"));
46+
Rf_setAttrib(*this, R_ClassSymbol, dateclass);
47+
}
48+
};
49+
}
50+
51+
#endif
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
//
3+
// newDatetimeVector.h: Rcpp R/C++ interface class library -- Datetime vector support
4+
//
5+
// Copyright (C) 2016 Dirk Eddelbuettel
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#ifndef Rcpp__newDatetimeVector_h
23+
#define Rcpp__newDatetimeVector_h
24+
25+
#include <RcppCommon.h>
26+
27+
namespace Rcpp {
28+
29+
class newDatetimeVector : public NumericVector {
30+
public:
31+
newDatetimeVector(SEXP vec, const char* tz = "") :
32+
NumericVector(vec) {
33+
setClass(tz);
34+
}
35+
newDatetimeVector(int n, const char* tz = "") :
36+
NumericVector(n) {
37+
setClass(tz);
38+
}
39+
40+
inline std::vector<Datetime> getDatetimes() const {
41+
int n = this->size();
42+
std::vector<Datetime> v(n);
43+
for (int i=0; i<n; i++)
44+
v[i] = (*this)[i];
45+
return v;
46+
}
47+
48+
private:
49+
50+
void setClass(const char *tz) {
51+
Shield<SEXP> datetimeclass(Rf_allocVector(STRSXP,2));
52+
SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXct"));
53+
SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXt"));
54+
Rf_setAttrib(*this, R_ClassSymbol, datetimeclass);
55+
56+
if (strcmp(tz, "") != 0) {
57+
Shield<SEXP> tzsexp(Rf_mkString(tz));
58+
Rf_setAttrib(*this, Rf_install("tzone"), tzsexp);
59+
}
60+
}
61+
};
62+
}
63+
64+
#endif

inst/include/Rcpp/DateVector.h renamed to inst/include/Rcpp/date_datetime/oldDateVector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
namespace Rcpp {
2929

30-
class DateVector : public GreedyVector<Date, DateVector> {
30+
class oldDateVector : public GreedyVector<Date, oldDateVector> {
3131
public:
32-
DateVector(SEXP vec) : GreedyVector<Date, DateVector>(vec) {}
33-
DateVector(int n) : GreedyVector<Date, DateVector>(n) {}
32+
oldDateVector(SEXP vec) : GreedyVector<Date, oldDateVector>(vec) {}
33+
oldDateVector(int n) : GreedyVector<Date, oldDateVector>(n) {}
3434

3535
inline std::vector<Date> getDates() const {
3636
return v ;

0 commit comments

Comments
 (0)