From 7c4a0537a8b99de030ef6cf0b3cecc9480e49ee8 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 22 Jan 2023 08:05:41 +0100 Subject: [PATCH 1/3] `write_excel_csv()` and `write_excel_csv()` put datetimes in a format that is readable again by `readr` on roundtrips. --- NEWS.md | 3 +++ R/write.R | 4 ++-- tests/testthat/test-write.R | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index cc817674..af46f094 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # readr (development version) +`write_excel_csv()` and `write_excel_csv()` put datetimes in a format that is +readable again by `readr` on roundtrips (#1202). + # readr 2.1.3 * Help files below `man/` have been re-generated, so that they give rise to valid HTML5. (This is the impetus for this release, to keep the package safely on CRAN.) diff --git a/R/write.R b/R/write.R index 5bf9700d..c8fe7fb3 100644 --- a/R/write.R +++ b/R/write.R @@ -193,7 +193,7 @@ write_excel_csv <- function(x, file, na = "NA", append = FALSE, x_out <- x datetime_cols <- vapply(x, inherits, logical(1), "POSIXt") - x[datetime_cols] <- lapply(x[datetime_cols], format, "%Y/%m/%d %H:%M:%S") + x[datetime_cols] <- lapply(x[datetime_cols], format, "%Y-%m-%d %H:%M:%S") x[] <- lapply(x, output_column) if (edition_first()) { @@ -242,7 +242,7 @@ write_excel_csv2 <- function(x, file, na = "NA", append = FALSE, x <- change_decimal_separator(x, decimal_mark = ",") datetime_cols <- vapply(x, inherits, logical(1), "POSIXt") - x[datetime_cols] <- lapply(x[datetime_cols], format, "%Y/%m/%d %H:%M:%S") + x[datetime_cols] <- lapply(x[datetime_cols], format, "%Y-%m-%d %H:%M:%S") x[] <- lapply(x, output_column) write_excel_csv(x, file, na, append, col_names, delim, diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index f8d20eff..c620e2f6 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -103,6 +103,28 @@ test_that("write_excel_csv/csv2 includes a byte order mark, but not when appendi expect_equal(output[-1:-3], charToRaw('"a"\n1\n2\n')) }) +test_that("write_excel_csv/csv2 preserves datetimes on roundtrip", { + tmp <- tempfile() + on.exit(unlink(tmp)) + + tmp2 <- tempfile() + on.exit(unlink(tmp2)) + + x <- as.POSIXct("2010-01-01 00:00:00") + 1:10 + attr(x, "tzone") <- "UTC" + + input <- data.frame(x = x) + + write_excel_csv(input, tmp) + write_excel_csv2(input, tmp2) + + output <- read_csv(tmp) + output2 <- read_csv2(tmp2) + + expect_equal(output$x, x) + expect_equal(output2$x, x) +}) + test_that("does not writes a tailing .0 for whole number doubles", { expect_equal(format_tsv(tibble::tibble(x = 1)), "x\n1\n") From e1fc50a7f893762d7dd2a0174babfe18606a0107 Mon Sep 17 00:00:00 2001 From: keesdeschepper Date: Sun, 22 Jan 2023 09:38:19 +0100 Subject: [PATCH 2/3] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index af46f094..564f8093 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # readr (development version) -`write_excel_csv()` and `write_excel_csv()` put datetimes in a format that is +`write_excel_csv()` and `write_excel_csv2()` put datetimes in a format that is readable again by `readr` on roundtrips (#1202). # readr 2.1.3 From b5d1c6834554d62f415eece0c9a7f9fe5402e74e Mon Sep 17 00:00:00 2001 From: keesdeschepper Date: Mon, 23 Jan 2023 12:55:46 +0100 Subject: [PATCH 3/3] Update NEWS.md --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 564f8093..46ba4df7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # readr (development version) -`write_excel_csv()` and `write_excel_csv2()` put datetimes in a format that is -readable again by `readr` on roundtrips (#1202). +* `write_excel_csv()` and `write_excel_csv2()` put datetimes in a format that is + readable again by `readr` on roundtrips (#1202). # readr 2.1.3