diff --git a/NEWS.md b/NEWS.md index f40ef71b..46ba4df7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # readr (development version) -* No user-facing changes. Patch release with internal changes requested by CRAN. +* `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 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")