From bbeb4c67808dc32bfea086c388efb0dfc73f56f8 Mon Sep 17 00:00:00 2001 From: olivroy Date: Mon, 16 Dec 2024 09:54:47 -0500 Subject: [PATCH 1/3] support printing of datetime without seconds --- R/shaft-.R | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/R/shaft-.R b/R/shaft-.R index f5d475857..c14f8e53e 100644 --- a/R/shaft-.R +++ b/R/shaft-.R @@ -276,17 +276,27 @@ pillar_shaft.POSIXt <- function(x, ...) { date <- format(x, format = "%Y-%m-%d") time <- format(x, format = "%H:%M:%OS") + time_short <- format(x, format = "%H:%M") + time_styled <- paste0(" ", style_subtle(time)) + time_short_styled <- paste0(" ", style_subtle(time_short)) # R < 4.3 don't support is.finite() for POSIXlt - time_styled <- paste0(" ", style_subtle(time)) if (inherits(x, "POSIXct")) { time_styled[!is.finite(x)] <- "" + time_short_styled[!is.finite(x)] <- "" } datetime <- paste0(date, time_styled) + datetime_short <- paste0(date, time_short_styled) datetime[is.na(x)] <- NA + datetime_short[is.na(x)] <- NA - new_pillar_shaft_simple(datetime, width = width, align = "left") + new_pillar_shaft_simple( + datetime, + # width = width, + align = "left", + short_formatted = datetime_short + ) } From ac97888cc4d9262a52f943ead02f9bf976ab60e4 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 18 Dec 2024 10:16:25 -0500 Subject: [PATCH 2/3] Try to address comments from review. --- NAMESPACE | 1 + R/shaft-.R | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 0a0783231..d834b0ab8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -20,6 +20,7 @@ S3method(format,pillar_rif_title) S3method(format,pillar_rif_type) S3method(format,pillar_shaft) S3method(format,pillar_shaft_decimal) +S3method(format,pillar_shaft_posixt) S3method(format,pillar_shaft_simple) S3method(format,pillar_squeezed_colonnade) S3method(format,pillar_tbl_format_setup) diff --git a/R/shaft-.R b/R/shaft-.R index c14f8e53e..cacf36352 100644 --- a/R/shaft-.R +++ b/R/shaft-.R @@ -291,14 +291,25 @@ pillar_shaft.POSIXt <- function(x, ...) { datetime[is.na(x)] <- NA datetime_short[is.na(x)] <- NA - new_pillar_shaft_simple( - datetime, - # width = width, - align = "left", - short_formatted = datetime_short + new_pillar_shaft( + list(datetime = datetime, datetime_min = datetime_short), + #align = "left", + width = get_max_extent(datetime_short), + min_width = get_max_extent(datetime), + class = "pillar_shaft_posixt" ) } +#' @export +format.pillar_shaft_posixt <- function(x, width, ...) { + if (get_max_extent(x$datetime_min) <= width) { + ornament <- x$datetime_min + } else { + ornament <- x$datetime + } + + new_ornament(ornament, align = "left") +} #' @export #' @rdname pillar_shaft From 51e90a876337a0b32885169b7553904b61ed7261 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 18 Dec 2024 10:16:52 -0500 Subject: [PATCH 3/3] Update snapshots --- tests/testthat/_snaps/ctl_colonnade.md | 105 +++++++++++------------ tests/testthat/_snaps/format_time.md | 18 ++-- tests/testthat/_snaps/tbl-format-body.md | 43 ++++------ 3 files changed, 78 insertions(+), 88 deletions(-) diff --git a/tests/testthat/_snaps/ctl_colonnade.md b/tests/testthat/_snaps/ctl_colonnade.md index e4eef9a48..d2faca282 100644 --- a/tests/testthat/_snaps/ctl_colonnade.md +++ b/tests/testthat/_snaps/ctl_colonnade.md @@ -210,16 +210,11 @@ ctl_colonnade(df_all, width = 300) Output $body - a b c d e f g h - - 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51:35 - 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51:36 - 3 NA NA NA NA NA - i - - 1 - 2 - 3 + a b c d e f g h i + + 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51 + 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51 + 3 NA NA NA NA NA $extra_cols list() @@ -235,16 +230,16 @@ ctl_colonnade(df_all, width = 300) Output $body - a b c d e f g - - 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51:35 - 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51:36 - 3 NA NA NA NA NA - h i - - 1 - 2 - 3 + a b c d e f g h + + 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51 + 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51 + 3 NA NA NA NA NA + i + + 1 + 2 + 3 $extra_cols list() @@ -260,16 +255,16 @@ ctl_colonnade(df_all, width = 300) Output $body - a b c d e f - - 1 1 1 TRUE a a 2015-12-10 - 2 2.5 2 FALSE b b 2015-12-11 - 3 NA NA NA NA - g h i - - 1 2015-12-09 10:51:35 - 2 2015-12-09 10:51:36 - 3 NA + a b c d e f g + + 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51 + 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51 + 3 NA NA NA NA NA + h i + + 1 + 2 + 3 $extra_cols list() @@ -290,11 +285,11 @@ 1 1 1 TRUE a a 2015-12-10 2 2.5 2 FALSE b b 2015-12-11 3 NA NA NA NA - g h i - - 1 2015-12-09 10:51:35 - 2 2015-12-09 10:51:36 - 3 NA + g h i + + 1 2015-12-09 10:51 + 2 2015-12-09 10:51 + 3 NA $extra_cols list() @@ -315,16 +310,16 @@ 1 1 1 TRUE a a 2 2.5 2 FALSE b b 3 NA NA NA - f g - - 1 2015-12-10 2015-12-09 10:51:35 - 2 2015-12-11 2015-12-09 10:51:36 - 3 NA NA - h i - - 1 - 2 - 3 + f g h + + 1 2015-12-10 2015-12-09 10:51 + 2 2015-12-11 2015-12-09 10:51 + 3 NA NA + i + + 1 + 2 + 3 $extra_cols list() @@ -350,16 +345,16 @@ 1 a 2015-12-10 2 b 2015-12-11 3 NA - g - - 1 2015-12-09 10:51:35 - 2 2015-12-09 10:51:36 - 3 NA - h i - - 1 - 2 - 3 + g h + + 1 2015-12-09 10:51 + 2 2015-12-09 10:51 + 3 NA + i + + 1 + 2 + 3 $extra_cols list() diff --git a/tests/testthat/_snaps/format_time.md b/tests/testthat/_snaps/format_time.md index f2d6ff905..378ba01cd 100644 --- a/tests/testthat/_snaps/format_time.md +++ b/tests/testthat/_snaps/format_time.md @@ -4,16 +4,16 @@ pillar(add_special(as.POSIXct("2017-07-28 18:04:35 +0200"))) Output - - 2017-07-28 18:04:35 - NA + + 2017-07-28 18:04 + NA Code pillar(add_special(as.POSIXlt("2017-07-28 18:04:35 +0200"))) Output - - 2017-07-28 18:04:35 - NA + + 2017-07-28 18:04 + NA --- @@ -21,9 +21,9 @@ pillar(add_special(as.POSIXlt("2017-07-28 18:04:35 +0200"))) Output - - 2017-07-28 18:04:35.0000 - NA + + 2017-07-28 18:04 + NA --- diff --git a/tests/testthat/_snaps/tbl-format-body.md b/tests/testthat/_snaps/tbl-format-body.md index 8e2d3ef5e..afdf8ebfc 100644 --- a/tests/testthat/_snaps/tbl-format-body.md +++ b/tests/testthat/_snaps/tbl-format-body.md @@ -14,16 +14,11 @@ tbl_format_body(tbl_format_setup(df_all, width = 300)) Output - a b c d e f g h - - 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51:35 - 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51:36 - 3 NA NA NA NA NA - i - - 1 - 2 - 3 + a b c d e f g h i + + 1 1 1 TRUE a a 2015-12-10 2015-12-09 10:51 + 2 2.5 2 FALSE b b 2015-12-11 2015-12-09 10:51 + 3 NA NA NA NA NA Code # POSIXct and POSIXlt df <- new_tbl(list(x = as.POSIXct("2016-01-01 12:34:56 GMT") + 1:12)) @@ -31,20 +26,20 @@ tbl_format_body(tbl_format_setup(df, width = 60L)) Output - x y - - 1 2016-01-01 12:34:57 2016-01-01 12:34:57 - 2 2016-01-01 12:34:58 2016-01-01 12:34:58 - 3 2016-01-01 12:34:59 2016-01-01 12:34:59 - 4 2016-01-01 12:35:00 2016-01-01 12:35:00 - 5 2016-01-01 12:35:01 2016-01-01 12:35:01 - 6 2016-01-01 12:35:02 2016-01-01 12:35:02 - 7 2016-01-01 12:35:03 2016-01-01 12:35:03 - 8 2016-01-01 12:35:04 2016-01-01 12:35:04 - 9 2016-01-01 12:35:05 2016-01-01 12:35:05 - 10 2016-01-01 12:35:06 2016-01-01 12:35:06 - 11 2016-01-01 12:35:07 2016-01-01 12:35:07 - 12 2016-01-01 12:35:08 2016-01-01 12:35:08 + x y + + 1 2016-01-01 12:34 2016-01-01 12:34 + 2 2016-01-01 12:34 2016-01-01 12:34 + 3 2016-01-01 12:34 2016-01-01 12:34 + 4 2016-01-01 12:35 2016-01-01 12:35 + 5 2016-01-01 12:35 2016-01-01 12:35 + 6 2016-01-01 12:35 2016-01-01 12:35 + 7 2016-01-01 12:35 2016-01-01 12:35 + 8 2016-01-01 12:35 2016-01-01 12:35 + 9 2016-01-01 12:35 2016-01-01 12:35 + 10 2016-01-01 12:35 2016-01-01 12:35 + 11 2016-01-01 12:35 2016-01-01 12:35 + 12 2016-01-01 12:35 2016-01-01 12:35 Code # Colonnade ctl_colonnade(list(a = 1:3), has_row_id = FALSE)