Skip to content

Commit f0442a0

Browse files
authored
Merge pull request #621 from RcppCore/feature/update_date_code
Feature/update date code
2 parents 4a9c6a8 + 86b36b0 commit f0442a0

File tree

4 files changed

+696
-513
lines changed

4 files changed

+696
-513
lines changed

ChangeLog

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2017-01-01 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/unitTests/runit.Date.R (test.mktime, test.gmtime): New tests
4+
* inst/unitTests/cpp/dates.cpp (test_mktime, test_gmtime): Idem
5+
16
2016-12-31 Dirk Eddelbuettel <[email protected]>
27

38
* inst/include/Rcpp/vector/Matrix.h: Minor simplification
@@ -10,6 +15,13 @@
1015
filling matrices.
1116
* inst/unitTests/cpp/Matrix.cpp: Idem
1217

18+
2016-12-30 Dirk Eddelbuettel <[email protected]>
19+
20+
* src/Date.cpp: Synchronized internal code with R
21+
22+
* inst/unitTests/cpp/dates.cpp (gmtime_mktime): New test
23+
* inst/unitTests/runit.Date.R (test.mktime_gmtime): Idem
24+
1325
2016-12-26 Dirk Eddelbuettel <[email protected]>
1426

1527
* R/Attributes.R: Added #nocov markers

inst/unitTests/cpp/dates.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,41 @@ std::string Datetime_ostream(Datetime d) {
196196
os << d;
197197
return os.str();
198198
}
199+
200+
// [[Rcpp::export]]
201+
Date gmtime_mktime(Date d) {
202+
const int baseYear = 1900;
203+
struct tm tm;
204+
tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_isdst = 0;
205+
206+
tm.tm_mday = d.getDay();
207+
tm.tm_mon = d.getMonth() - 1; // range 0 to 11
208+
tm.tm_year = d.getYear() - baseYear;
209+
time_t tmp = mktime00(tm); // use mktime() replacement borrowed from R
210+
211+
struct tm chk = *gmtime_(&tmp);
212+
Date newd(chk.tm_year, chk.tm_mon + 1, chk.tm_mday);
213+
return newd;
214+
}
215+
216+
// [[Rcpp::export]]
217+
double test_mktime(Date d) {
218+
const int baseYear = 1900;
219+
struct tm tm;
220+
tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_isdst = 0;
221+
222+
tm.tm_mday = d.getDay();
223+
tm.tm_mon = d.getMonth() - 1; // range 0 to 11
224+
tm.tm_year = d.getYear() - baseYear;
225+
time_t t = mktime00(tm); // use mktime() replacement borrowed from R
226+
return static_cast<double>(t);
227+
}
228+
229+
// [[Rcpp::export]]
230+
Date test_gmtime(double d) {
231+
time_t t = static_cast<time_t>(d);
232+
struct tm tm = *gmtime_(&t);
233+
tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_isdst = 0;
234+
Date nd(tm.tm_year, tm.tm_mon + 1, tm.tm_mday);
235+
return nd;
236+
}

inst/unitTests/runit.Date.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,33 @@ if (.runThisTest) {
239239
}
240240

241241

242+
test.mktime_gmtime <- function() {
243+
d <- as.Date("2015-12-31")
244+
checkEquals(d, gmtime_mktime(d), msg="Date.mktime_gmtime.2015")
245+
246+
d <- as.Date("1965-12-31")
247+
checkEquals(d, gmtime_mktime(d), msg="Date.mktime_gmtime.1965")
248+
}
249+
250+
test.mktime <- function() {
251+
d <- as.Date("2015-12-31")
252+
checkEquals(test_mktime(d), as.numeric(as.POSIXct(d)), msg="Date.test_mktime.2015")
253+
254+
d <- as.Date("1970-01-01")
255+
checkEquals(test_mktime(d), as.numeric(as.POSIXct(d)), msg="Date.test_mktime.1970")
256+
257+
d <- as.Date("1954-07-04")
258+
checkEquals(test_mktime(d), as.numeric(as.POSIXct(d)), msg="Date.test_mktime.1954")
259+
}
260+
261+
test.gmtime <- function() {
262+
oldTZ <- Sys.getenv("TZ")
263+
Sys.setenv(TZ="UTC")
264+
checkEquals(test_gmtime(1441065600), as.Date("2015-09-01"), msg="Date.test_gmtime.2015")
265+
266+
checkEquals(test_gmtime(0), as.Date("1970-01-01"), msg="Date.test_gmtime.1970")
267+
268+
checkEquals(test_gmtime(-489024000), as.Date("1954-07-04"), msg="Date.test_gmtime.1954")
269+
Sys.setenv(TZ=oldTZ)
270+
}
242271
}

0 commit comments

Comments
 (0)