Skip to content

Commit 0c7569a

Browse files
committed
a new test (which also passes in master)
1 parent cd5a69b commit 0c7569a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
* src/Date.cpp: Synchronized internal code with R
44

5+
* inst/unitTests/cpp/dates.cpp (gmtime_mktime): New test
6+
* inst/unitTests/runit.Date.R (test.mktime_gmtime): Idem
7+
58
2016-12-26 Dirk Eddelbuettel <[email protected]>
69

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

inst/unitTests/cpp/dates.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,19 @@ 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+
}

inst/unitTests/runit.Date.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,11 @@ 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+
}
242249
}

0 commit comments

Comments
 (0)