Skip to content

Commit 86b36b0

Browse files
committed
two new tests
1 parent ffbe61d commit 86b36b0

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ChangeLog

Lines changed: 5 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

inst/unitTests/cpp/dates.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,25 @@ Date gmtime_mktime(Date d) {
212212
Date newd(chk.tm_year, chk.tm_mon + 1, chk.tm_mday);
213213
return newd;
214214
}
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,26 @@ if (.runThisTest) {
246246
d <- as.Date("1965-12-31")
247247
checkEquals(d, gmtime_mktime(d), msg="Date.mktime_gmtime.1965")
248248
}
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+
}
249271
}

0 commit comments

Comments
 (0)