Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_excel_csv() and write_excel_csv2() put datetimes in a format… #1463

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down