Skip to content

Commit 8748eb6

Browse files
mv Datetime to headers
1 parent 6ea0147 commit 8748eb6

File tree

6 files changed

+99
-88
lines changed

6 files changed

+99
-88
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 0.10.6
3+
Version: 0.10.6.1
44
Date: $Date$
55
Author: Dirk Eddelbuettel and Romain Francois, with contributions
66
by Douglas Bates, John Chambers and JJ Allaire

inst/include/Rcpp/Datetime.h

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// under the terms of the GNU General Public License as published by
1111
// the Free Software Foundation, either version 2 of the License, or
1212
// (at your option) any later version.
13-
//
13+
//
1414
// Rcpp is distributed in the hope that it will be useful, but
1515
// WITHOUT ANY WARRANTY; without even the implied warranty of
1616
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -28,12 +28,18 @@ namespace Rcpp {
2828

2929
class Datetime {
3030
public:
31-
Datetime();
32-
Datetime(SEXP s);
33-
Datetime(const double &dt); // from double, just like POSIXct
31+
Datetime() {
32+
m_dt = 0;
33+
update_tm();
34+
}
35+
Datetime(SEXP s);
36+
37+
// from double, just like POSIXct
38+
Datetime(const double &dt){
39+
m_dt = dt;
40+
update_tm();
41+
}
3442
Datetime(const std::string &s, const std::string &fmt="%Y-%m-%d %H:%M:%OS");
35-
Datetime(const Datetime &copy);
36-
~Datetime() {};
3743

3844
double getFractionalTimestamp(void) const { return m_dt; }
3945

@@ -47,8 +53,6 @@ namespace Rcpp {
4753
int getWeekday() const { return m_tm.tm_wday + 1; } // makes it 1 .. 7
4854
int getYearday() const { return m_tm.tm_yday + 1; } // makes it 1 .. 366
4955

50-
Datetime & operator=(const Datetime &newdt); // copy assignment operator
51-
5256
// Minimal set of date operations.
5357
friend Datetime operator+(const Datetime &dt, double offset);
5458
friend double operator-(const Datetime& dt1, const Datetime& dt2);
@@ -66,7 +70,20 @@ namespace Rcpp {
6670
struct tm m_tm; // standard time representation
6771
unsigned int m_us; // microsecond (to complement m_tm)
6872

69-
void update_tm(); // update m_tm based on m_dt
73+
// update m_tm based on m_dt
74+
void update_tm() {
75+
if (R_FINITE(m_dt)) {
76+
time_t t = static_cast<time_t>(std::floor(m_dt));
77+
m_tm = *gmtime(&t); // this may need a Windows fix, re-check R's datetime.c
78+
// m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm
79+
m_us = static_cast<int>(::Rf_fround( (m_dt - t) * 1.0e6, 0.0));
80+
} else {
81+
m_dt = NA_REAL; // NaN and Inf need it set
82+
m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
83+
m_tm.tm_min = m_tm.tm_hour = m_tm.tm_mday = m_tm.tm_mon = m_tm.tm_year = NA_INTEGER;
84+
m_us = NA_INTEGER;
85+
}
86+
}
7087

7188
};
7289

@@ -85,7 +102,24 @@ namespace Rcpp {
85102
}
86103

87104
template<> SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ) ;
88-
105+
106+
inline Datetime operator+(const Datetime &datetime, double offset) {
107+
Datetime newdt(datetime.m_dt);
108+
newdt.m_dt += offset;
109+
time_t t = static_cast<time_t>(std::floor(newdt.m_dt));
110+
newdt.m_tm = *gmtime(&t); // this may need a Windows fix, re-check R's dat
111+
newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - t) * 1.0e6, 0.0));
112+
return newdt;
113+
}
114+
115+
inline double operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; }
116+
inline bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }
117+
inline bool operator>(const Datetime &d1, const Datetime& d2) { return d1.m_dt > d2.m_dt; }
118+
inline bool operator==(const Datetime &d1, const Datetime& d2) { return d1.m_dt == d2.m_dt; }
119+
inline bool operator>=(const Datetime &d1, const Datetime& d2) { return d1.m_dt >= d2.m_dt; }
120+
inline bool operator<=(const Datetime &d1, const Datetime& d2) { return d1.m_dt <= d2.m_dt; }
121+
inline bool operator!=(const Datetime &d1, const Datetime& d2) { return d1.m_dt != d2.m_dt; }
122+
89123
}
90124

91125
#endif
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
2+
//
3+
// This file is part of Rcpp.
4+
//
5+
// Rcpp is free software: you can redistribute it and/or modify it
6+
// under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Rcpp is distributed in the hope that it will be useful, but
11+
// WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#ifndef Rcpp_api_meat_Datetime_h
19+
#define Rcpp_api_meat_Datetime_h
20+
21+
namespace Rcpp{
22+
23+
inline Datetime::Datetime(SEXP d) {
24+
m_dt = Rcpp::as<double>(d);
25+
update_tm();
26+
}
27+
28+
inline Datetime::Datetime(const std::string &s, const std::string &fmt) {
29+
Rcpp::Function strptime("strptime"); // we cheat and call strptime() from R
30+
Rcpp::Function asPOSIXct("as.POSIXct"); // and we need to convert to POSIXct
31+
m_dt = Rcpp::as<double>(asPOSIXct(strptime(s, fmt)));
32+
update_tm();
33+
}
34+
35+
template<>
36+
inline SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ){
37+
Rf_setAttrib(x, R_ClassSymbol, internal::getPosixClasses() );
38+
return x ;
39+
}
40+
41+
template <>
42+
inline SEXP wrap<Datetime>(const Datetime &date) {
43+
return internal::new_posixt_object( date.getFractionalTimestamp() ) ;
44+
}
45+
46+
47+
}
48+
49+
#endif

inst/include/Rcpp/api/meat/meat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <Rcpp/api/meat/SlotProxy.h>
3232

3333
#include <Rcpp/api/meat/Date.h>
34+
#include <Rcpp/api/meat/Datetime.h>
3435
#include <Rcpp/api/meat/DataFrame.h>
3536
#include <Rcpp/api/meat/S4.h>
3637
#include <Rcpp/api/meat/Environment.h>

inst/include/RcppCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ namespace Rcpp{
7676
#include <typeinfo>
7777
#include <Rcpp/sprintf.h>
7878
#include <R_ext/Callbacks.h>
79+
#include <Rmath.h> // for Rf_fround
7980

8081
namespace Rcpp{
8182
class Module ;

src/Date.cpp

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -83,83 +83,9 @@ namespace Rcpp {
8383
#undef isleap
8484
#undef days_in_year
8585

86-
Datetime::Datetime() {
87-
m_dt = 0;
88-
update_tm();
89-
}
90-
91-
Datetime::Datetime(SEXP d) {
92-
m_dt = Rcpp::as<double>(d);
93-
update_tm();
94-
}
95-
96-
Datetime::Datetime(const double &dt) {
97-
m_dt = dt;
98-
update_tm();
99-
}
100-
101-
Datetime::Datetime(const std::string &s, const std::string &fmt) {
102-
Rcpp::Function strptime("strptime"); // we cheat and call strptime() from R
103-
Rcpp::Function asPOSIXct("as.POSIXct"); // and we need to convert to POSIXct
104-
m_dt = Rcpp::as<double>(asPOSIXct(strptime(s, fmt)));
105-
update_tm();
106-
}
107-
108-
Datetime::Datetime(const Datetime &copy) {
109-
m_dt = copy.m_dt;
110-
m_us = copy.m_us;
111-
m_tm = copy.m_tm;
112-
}
113-
114-
Datetime & Datetime::operator=(const Datetime & newdt) {
115-
if (this != &newdt) {
116-
m_dt = newdt.m_dt;
117-
m_us = newdt.m_us;
118-
m_tm = newdt.m_tm;
119-
}
120-
return *this;
121-
}
122-
123-
void Datetime::update_tm() {
124-
if (R_FINITE(m_dt)) {
125-
time_t t = static_cast<time_t>(std::floor(m_dt));
126-
m_tm = *gmtime(&t); // this may need a Windows fix, re-check R's datetime.c
127-
// m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm
128-
m_us = static_cast<int>(::Rf_fround( (m_dt - t) * 1.0e6, 0.0));
129-
} else {
130-
m_dt = NA_REAL; // NaN and Inf need it set
131-
m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
132-
m_tm.tm_min = m_tm.tm_hour = m_tm.tm_mday = m_tm.tm_mon = m_tm.tm_year = NA_INTEGER;
133-
m_us = NA_INTEGER;
134-
}
135-
}
136-
137-
Datetime operator+(const Datetime &datetime, double offset) {
138-
Datetime newdt(datetime.m_dt);
139-
newdt.m_dt += offset;
140-
time_t t = static_cast<time_t>(std::floor(newdt.m_dt));
141-
newdt.m_tm = *gmtime(&t); // this may need a Windows fix, re-check R's dat
142-
newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - t) * 1.0e6, 0.0));
143-
return newdt;
144-
}
145-
146-
double operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; }
147-
bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }
148-
bool operator>(const Datetime &d1, const Datetime& d2) { return d1.m_dt > d2.m_dt; }
149-
bool operator==(const Datetime &d1, const Datetime& d2) { return d1.m_dt == d2.m_dt; }
150-
bool operator>=(const Datetime &d1, const Datetime& d2) { return d1.m_dt >= d2.m_dt; }
151-
bool operator<=(const Datetime &d1, const Datetime& d2) { return d1.m_dt <= d2.m_dt; }
152-
bool operator!=(const Datetime &d1, const Datetime& d2) { return d1.m_dt != d2.m_dt; }
153-
154-
template<> SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ){
155-
Rf_setAttrib(x, R_ClassSymbol, internal::getPosixClasses() );
156-
return x ;
157-
}
158-
159-
template <> SEXP wrap(const Datetime &date) {
160-
return internal::new_posixt_object( date.getFractionalTimestamp() ) ;
161-
}
162-
DatetimeVector::DatetimeVector(SEXP vec) : v() {
86+
87+
88+
DatetimeVector::DatetimeVector(SEXP vec) : v() {
16389
int i;
16490
if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
16591
throw std::range_error("DatetimeVector: invalid numeric vector in constructor");

0 commit comments

Comments
 (0)