Skip to content

Commit 06fb890

Browse files
committed
unit tests for Date(time) formatters
1 parent b264d8a commit 06fb890

File tree

4 files changed

+283
-230
lines changed

4 files changed

+283
-230
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2016-11-28 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/unitTests/cpp/dates.cpp (Date_format, Datetime_format): New tests
4+
* inst/unitTests/runit.Date.R (test.Date.formating): Ditto
5+
(test.Datetime.formating): Ditto
6+
17
2016-11-27 Dirk Eddelbuettel <[email protected]>
28

39
* inst/include/Rcpp/date_datetime/Date.h (format, operator<<): Added

inst/unitTests/cpp/dates.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,14 @@ DatetimeVector DatetimeVector_assignment(DatetimeVector v1,
171171
DateVector DateVector_assignment(DateVector v1, DateVector v2) {
172172
v1 = v2;
173173
return v1;
174-
}
174+
}
175+
176+
// [[Rcpp::export]]
177+
std::string Date_format(Date d, std::string fmt) {
178+
return d.format(fmt.c_str());
179+
}
180+
181+
// [[Rcpp::export]]
182+
std::string Datetime_format(Datetime d, std::string fmt) {
183+
return d.format(fmt.c_str());
184+
}

inst/unitTests/runit.Date.R

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,56 @@ if (.runThisTest) {
181181
checkEquals(fun(vec), c(now, rep(posixtNA, 3), now+2.345), msg = "Datetime.ctor.NA.NaN.Inf.set")
182182
}
183183
}
184-
184+
185185
test.DatetimeVector.assignment <- function() {
186186
now <- Sys.time()
187187
v1 <- c(now, now + 1, now + 2)
188188
v2 <- c(now + 3, now + 4, now + 5)
189189
checkEquals(v2, DatetimeVector_assignment(v1, v2))
190190
}
191-
191+
192192
test.DateVector.assignment <- function() {
193193
now <- Sys.Date()
194194
v1 <- c(now, now + 1, now + 2)
195195
v2 <- c(now + 3, now + 4, now + 5)
196196
checkEquals(v2, DateVector_assignment(v1, v2))
197197
}
198198

199+
200+
## formatting
201+
test.Date.formating <- function() {
202+
oldTZ <- Sys.getenv("TZ")
203+
Sys.setenv(TZ="America/Chicago")
204+
d <- as.Date("2011-12-13")
205+
206+
checkEquals(Date_format(d, "%Y-%m-%d"),
207+
format(d),
208+
msg="Date.formating.default")
209+
checkEquals(Date_format(d, "%Y/%m/%d"),
210+
format(d, "%Y/%m/%d"),
211+
msg="Date.formating.given.format")
212+
213+
Sys.setenv(TZ=oldTZ)
214+
}
215+
216+
test.Datetime.formating <- function() {
217+
oldTZ <- Sys.getenv("TZ")
218+
Sys.setenv(TZ="America/Chicago")
219+
220+
olddigits <- getOption("digits.secs")
221+
options("digits.secs"=6)
222+
223+
d <- as.POSIXct("2016-12-13 14:15:16.123456")
224+
checkEquals(Datetime_format(d,"%Y-%m-%d %H:%M:%S"),
225+
format(d, "%Y-%m-%d %H:%M:%OS"),
226+
msg="Datetime.formating.default")
227+
checkEquals(Datetime_format(d, "%Y/%m/%d %H:%M:%S"),
228+
format(d, "%Y/%m/%d %H:%M:%OS"),
229+
msg="Datetime.formating.given.format")
230+
231+
Sys.setenv(TZ=oldTZ)
232+
options("digits.secs"=olddigits)
233+
}
234+
235+
199236
}

0 commit comments

Comments
 (0)