Skip to content

Commit 6ea0147

Browse files
mv Date to headers
1 parent 5b887a4 commit 6ea0147

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

inst/include/Rcpp/Date.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ namespace Rcpp {
4949
m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = 0;
5050

5151
// allow for ISO-notation case (yyyy, mm, dd) which we prefer over (mm, dd, year)
52-
if (mon >= baseYear && day <= 12 && year <= 31) {
53-
m_tm.tm_year = mon - baseYear;
52+
if (mon >= baseYear() && day <= 12 && year <= 31) {
53+
m_tm.tm_year = mon - baseYear();
5454
m_tm.tm_mon = day - 1; // range 0 to 11
5555
m_tm.tm_mday = year;
5656
} else {
5757
m_tm.tm_mday = day;
5858
m_tm.tm_mon = mon - 1; // range 0 to 11
59-
m_tm.tm_year = year - baseYear;
59+
m_tm.tm_year = year - baseYear();
6060
}
6161
double tmp = mktime00(m_tm); // use mktime() replacement borrowed from R
62-
m_tm.tm_year += baseYear; // we'd rather keep it as a normal year
62+
m_tm.tm_year += baseYear() ; // we'd rather keep it as a normal year
6363
m_d = tmp/(24*60*60);
6464
}
6565

@@ -79,10 +79,10 @@ namespace Rcpp {
7979
int getWeekday() const { return m_tm.tm_wday + 1; } // makes it 1 .. 7
8080
int getYearday() const { return m_tm.tm_yday + 1; } // makes it 1 .. 366
8181

82-
static const unsigned int QLtoJan1970Offset; // Offset between R / Unix epoch date and the QL base date
83-
static const unsigned int baseYear; // 1900 as per POSIX mktime() et al
84-
85-
Date & operator=(const Date &newdate); // copy assignment operator
82+
// 1900 as per POSIX mktime() et al
83+
static inline const unsigned int baseYear(){
84+
return 1900 ;
85+
}
8686

8787
// Minimal set of date operations.
8888
friend Date operator+(const Date &date, int offset);
@@ -116,9 +116,6 @@ namespace Rcpp {
116116
double mktime00(struct tm &tm) const; // from R's src/main/datetime.c
117117
};
118118

119-
const unsigned int Date::QLtoJan1970Offset = 25569; // Offset between R / Unix epoch date and the QL base date
120-
const unsigned int Date::baseYear = 1900; // because we hate macros
121-
122119
// template specialisation for wrap() on the date
123120
template <> SEXP wrap<Rcpp::Date>(const Rcpp::Date &date);
124121

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_Date_h
19+
#define Rcpp_api_meat_Date_h
20+
21+
namespace Rcpp{
22+
23+
inline Date::Date(SEXP d) {
24+
m_d = Rcpp::as<double>(d);
25+
update_tm();
26+
}
27+
28+
inline Date::Date(const std::string &s, const std::string &fmt) {
29+
Function strptime("strptime"); // we cheat and call strptime() from R
30+
Function asDate("as.Date"); // and we need to convert to Date
31+
m_d = Rcpp::as<int>(asDate(strptime(s, fmt, "UTC")));
32+
update_tm();
33+
}
34+
35+
template <>
36+
inline SEXP wrap(const Date &date) {
37+
return internal::new_date_object( date.getDate() ) ;
38+
}
39+
40+
41+
}
42+
43+
#endif

inst/include/Rcpp/routines.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace Rcpp{
3434
SEXP get_Rcpp_namespace() ;
3535
SEXP rcpp_get_stack_trace() ;
3636
SEXP rcpp_set_stack_trace(SEXP) ;
37-
inline double mktime00(struct tm &) ;
38-
inline struct tm * gmtime_(const time_t * const) ;
37+
double mktime00(struct tm &) ;
38+
struct tm * gmtime_(const time_t * const) ;
3939
}
4040

4141
int* get_cache( int n ) ;

src/Date.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace Rcpp {
5151
double excess = 0.0;
5252

5353
day = tm.tm_mday - 1;
54-
year0 = Date::baseYear + tm.tm_year;
54+
year0 = Date::baseYear() + tm.tm_year;
5555
/* safety check for unbounded loops */
5656
if (year0 > 3000) {
5757
excess = (int)(year0/2000) - 1;

0 commit comments

Comments
 (0)