# Column spacing is incorrect when printing columns with fractional seconds #102

Closed
opened this Issue Mar 1, 2018 · 1 comment

Projects
2 participants

### DavisVaughan commented Mar 1, 2018 • edited

 Following discussion on #74. When the user sets `digits.secs` option, the fractional seconds are printed but the spacing is off because `width` is not adjusted dynamically. The reprex below outlines the problem and proposes a solution. ```# with dev pillar and tibble library(pillar) library(tibble) from <- as.POSIXct("14:03:55", format="%H:%M:%OS", tz="UTC") to <- as.POSIXct("14:04:00", format="%H:%M:%OS", tz="UTC") options(digits.secs = 4) ex <- tibble(datetime = seq(from, to, by = 0.01)) ex\$col <- 100000 # The correct amount of spacing is not used with fractional seconds print(ex, n = 2) #> # A tibble: 501 x 2 #> datetime col #> #> 1 2018-03-01 14:03:55.0000 100000. #> 2 2018-03-01 14:03:55.0099 100000. #> # ... with 499 more rows # Potential solution # This should really be pillar_shaft.POSIXt but had to do this for testing # FYI the digits.secs option allows values from 0-6 and defaults to NULL (0) pillar_shaft.POSIXct <- function (x, ...) { # Fractional seconds width adjustment width <- 19L digits.secs <- min(max(getOption("digits.secs"), 0L), 6L) # Clamp between 0-6 if (digits.secs) { width <- width + digits.secs + 1L # The 1L is for the extra decimal point } 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 = width, align = "left") } print(ex, n = 2) #> # A tibble: 501 x 2 #> datetime col #> #> 1 2018-03-01 14:03:55.0000 100000. #> 2 2018-03-01 14:03:55.0099 100000. #> # ... with 499 more rows # Even if the user does something crazy everything is alright... # The default options(digits.secs = NULL) head(ex, n = 1) #> # A tibble: 1 x 2 #> datetime col #> #> 1 2018-03-01 14:03:55 100000. # Negative options(digits.secs = -1) head(ex, n = 1) #> # A tibble: 1 x 2 #> datetime col #> #> 1 2018-03-01 14:03:55 100000. # Really big (max of 6) options(digits.secs = 12) head(ex, n = 1) #> # A tibble: 1 x 2 #> datetime col #> #> 1 2018-03-01 14:03:55.000000 100000.``` Created on 2018-03-01 by the reprex package (v0.1.1.9000).

Open

Member

### krlmlr commented Apr 16, 2018

 Thanks for raising this, and for the suggested fix.