|
| 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 |
0 commit comments