Rcpp Version 1.1.2
Loading...
Searching...
No Matches
newDatetimeVector.h
Go to the documentation of this file.
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
27namespace Rcpp {
28
30 public:
31 newDatetimeVector(const char* tz = "") : NumericVector() {
32 setClass(tz);
33 }
34
35 template <int RTYPE, bool NA, typename VEC>
36 newDatetimeVector(const VectorBase<RTYPE,NA,VEC>& other, const char* tz = "") :
37 NumericVector(other) {
38 setClass(tz);
39 }
40
41 newDatetimeVector(SEXP vec, const char* tz = "") :
42 NumericVector(vec) {
43 setClass(tz);
44 }
45
46 newDatetimeVector(int n, const char* tz = "") :
47 NumericVector(n) {
48 setClass(tz);
49 }
50
51 inline std::vector<Datetime> getDatetimes() const {
52 size_t n = this->size();
53 std::vector<Datetime> v(n);
54 for (size_t i=0; i<n; i++)
55 v[i] = (*this)[i];
56 return v;
57 }
58
60 if (this != &rhs) {
62 this->attr("tzone") = rhs.attr("tzone");
63 }
64
65 return *this;
66 }
67
68 friend inline std::ostream &operator<<(std::ostream & s, const newDatetimeVector d);
69
70 private:
71
72 void setClass(const char *tz) {
73 Shield<SEXP> datetimeclass(Rf_allocVector(STRSXP,2));
74 SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXct"));
75 SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXt"));
76 Rf_setAttrib(*this, R_ClassSymbol, datetimeclass);
77
78 if (strcmp(tz, "") != 0) {
79 Shield<SEXP> tzsexp(Rf_mkString(tz));
80 Rf_setAttrib(*this, Rf_install("tzone"), tzsexp);
81 }
82 }
83 };
84
85 inline std::ostream &operator<<(std::ostream & os, const newDatetimeVector d) {
86 size_t n = d.size();
87 for (size_t i=0; i<n; i++) {
88 os << Datetime(d[i]).format() << " ";
89 if ((i+1) % 4 == 0) os << "\n";
90 }
91 return os;
92 }
93
94}
95
96#endif
std::string format(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition Datetime.h:74
R_xlen_t size() const
Definition Vector.h:274
Vector & operator=(const Vector &rhs)
Definition Vector.h:68
newDatetimeVector(SEXP vec, const char *tz="")
newDatetimeVector(int n, const char *tz="")
newDatetimeVector(const char *tz="")
newDatetimeVector(const VectorBase< RTYPE, NA, VEC > &other, const char *tz="")
newDatetimeVector & operator=(const newDatetimeVector &rhs)
std::vector< Datetime > getDatetimes() const
friend std::ostream & operator<<(std::ostream &s, const newDatetimeVector d)
void setClass(const char *tz)
Rcpp API.
Definition algo.h:28
Vector< REALSXP > NumericVector
std::ostream & operator<<(std::ostream &os, const Date d)
Definition Date.h:172