File tree Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
* src/Date.cpp: Synchronized internal code with R
4
4
5
+ * inst/unitTests/cpp/dates.cpp (gmtime_mktime): New test
6
+ * inst/unitTests/runit.Date.R (test.mktime_gmtime): Idem
7
+
5
8
2016-12-26 Dirk Eddelbuettel <
[email protected] >
6
9
7
10
* R/Attributes.R: Added #nocov markers
Original file line number Diff line number Diff line change @@ -196,3 +196,19 @@ std::string Datetime_ostream(Datetime d) {
196
196
os << d;
197
197
return os.str ();
198
198
}
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
+ }
Original file line number Diff line number Diff line change @@ -239,4 +239,11 @@ if (.runThisTest) {
239
239
}
240
240
241
241
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
+ }
242
249
}
You can’t perform that action at this time.
0 commit comments