From 3e8bf2409eb23914eca6b4d430748a2c7cdd9030 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 19 May 2017 11:18:48 -0400 Subject: [PATCH] Closes #2171 -- edge case missed in fix to #2032 --- NEWS.md | 2 +- R/IDateTime.R | 5 ++++- inst/tests/tests.Rraw | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1efb9ae438..9016c72fa1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,7 +34,7 @@ 7. Seg fault in `rbindlist()` when one or more items are empty, [#2019](https://github.com/Rdatatable/data.table/issues/2019). Thanks Michael Lang for the pull request. -8. Error printing 0-length `ITime` objects, [#2032](https://github.com/Rdatatable/data.table/issues/2032). Thanks Michael Chirico for the pull request. +8. Error printing 0-length `ITime` and `NA` objects, [#2032](https://github.com/Rdatatable/data.table/issues/2032) and [#2171](https://github.com/Rdatatable/data.table/issues/2171). Thanks Michael Chirico for the pull requests and @franknarf1 for pointing out a shortcoming of the initial fix. 9. `as.IDate.POSIXct` error with `NULL` timezone, [#1973](https://github.com/Rdatatable/data.table/issues/1973). Thanks @lbilli for reporting and Michael Chirico for the pull request. diff --git a/R/IDateTime.R b/R/IDateTime.R index 335b10168b..2a1a0540f8 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -121,7 +121,10 @@ as.character.ITime <- format.ITime <- function(x, ...) { x <- abs(unclass(x)) hh <- x %/% 3600L mm <- (x - hh * 3600L) %/% 60L - ss <- trunc(x - hh * 3600L - 60L * mm) + # #2171 -- trunc gives numeric but %02d requires integer; + # as.integer is also faster (but doesn't handle integer overflow) + # http://stackoverflow.com/questions/43894077 + ss <- as.integer(x - hh * 3600L - 60L * mm) res = sprintf('%02d:%02d:%02d', hh, mm, ss) # Fix for #1354, so that "NA" input is handled correctly. if (is.na(any(neg))) res[is.na(x)] = NA diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index cffa4764a0..b0cf30db24 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9909,7 +9909,9 @@ ans = data.table(id=c(1L,3L),a=c(1,2)) for (i in 1:100) test(1763, rbindlist(x, idcol="id"), ans) # as.ITime(character(0)) used to fail, #2032 -test(1764, capture.output(format(as.ITime(character(0)))), "character(0)") +test(1764.1, format(as.ITime(character(0))), character(0)) +# Edge case from #2171 +test(1764.2, format(structure(NA_integer_, class = "ITime")), NA_character_) # IDateTime error when tzone is NULL, #1973 x = as.POSIXct('2017-03-17')