Skip to content

Commit

Permalink
respect digits.secs option, closes #102
Browse files Browse the repository at this point in the history
- The `digits.secs` option is respected when computing the width for date-time values (#102).
  • Loading branch information
krlmlr committed Apr 16, 2018
1 parent 27420b9 commit b62549d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion R/shaft.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ pillar_shaft.Date <- function(x, ...) {
#' @export
#' @rdname pillar_shaft
pillar_shaft.POSIXt <- function(x, ...) {
width <- 19L
digits_secs <- getOption("digits.secs", 0L)
if (digits_secs > 0) {
width <- width + min(digits_secs, 6) + 1L
}

date <- format(x, format = "%Y-%m-%d")
time <- format(x, format = "%H:%M:%OS")

datetime <- paste0(date, " ", style_subtle(time))
datetime[is.na(x)] <- NA

new_pillar_shaft_simple(datetime, width = 19, align = "left")
new_pillar_shaft_simple(datetime, width = width, align = "left")
}


Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/bw-out/time-digits-secs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<S3: POSIXlt>
2017-07-28 18:04:35.0000
NA
3 changes: 3 additions & 0 deletions tests/testthat/out/time-digits-secs.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test-format_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ context("format_time")
test_that("output test", {
expect_pillar_output(as.POSIXct("2017-07-28 18:04:35 +0200"), filename = "time.txt")
expect_pillar_output(as.POSIXlt("2017-07-28 18:04:35 +0200"), filename = "time-posix.txt")
withr::with_options(
list(digits.secs = 4),
expect_pillar_output(as.POSIXlt("2017-07-28 18:04:35 +0200"), filename = "time-digits-secs.txt")
)
})

0 comments on commit b62549d

Please sign in to comment.