Skip to content

Commit

Permalink
[Fix #719] Print negative durations with leading -
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Dec 7, 2019
1 parent e24c685 commit 6675469
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 6 additions & 5 deletions R/durations.r
Expand Up @@ -82,11 +82,12 @@ compute_estimate <- function (secs, unit = "second") {
if (is.null(next_unit))
return(.readable_duration(secs, "year"))
out <- character(length(secs))
tt <- secs < SECONDS_IN_ONE[[next_unit]]
tt <- abs(secs) < SECONDS_IN_ONE[[next_unit]]
if (any(tt))
out[tt] <- .readable_duration(secs[tt], unit)
wnext <- which(!tt)
out[wnext] <- compute_estimate(secs[wnext], next_unit)
if (length(wnext))
out[wnext] <- compute_estimate(secs[wnext], next_unit)
out
}

Expand All @@ -101,10 +102,10 @@ setMethod("show", signature(object = "Duration"), function(object) {

#' @export
format.Duration <- function(x, ...) {
out <- vector("character", length(x@.Data))
out <- character(length(x@.Data))
nnas <- !is.na(x@.Data)
out[nnas] <- compute_estimate(abs(x@.Data[nnas]))
out[!nnas] <- NA
out[nnas] <- compute_estimate(x@.Data[nnas])
out[!nnas] <- NA_character_
out
}

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-am-pm.R
Expand Up @@ -10,6 +10,5 @@ test_that("am and pm handle various classes of date-time object", {
x <- as.POSIXct(c("2009-08-03 13:01:59", "2008-08-03 10:01:59"), tz = "UTC")
expect_equal(am(x), c(FALSE, TRUE))
expect_equal(am(as.POSIXlt(x)), c(FALSE, TRUE))

expect_equal(am(as.Date(x)), c(TRUE, TRUE))
})
10 changes: 6 additions & 4 deletions tests/testthat/test-durations.R
Expand Up @@ -173,10 +173,12 @@ test_that("is.duration works as expected", {
expect_false(is.duration(interval(lt_time, ct_time)))
})

test_that("format.Duration correctly displays durations with an NA", {
dur <- duration(seconds = c(5, NA))

expect_equivalent(format(dur), c("5s", NA))
test_that("format.Duration works as expected", {
dur <- duration(seconds = c(5, NA, 10, -10, 1000, -1000))
expect_equivalent(format(dur),
c("5s", NA, "10s", "-10s",
"1000s (~16.67 minutes)",
"-1000s (~-16.67 minutes)"))
})

test_that("summary.Duration creates useful summary", {
Expand Down

0 comments on commit 6675469

Please sign in to comment.