diff --git a/src/DfReader.cpp b/src/DfReader.cpp index eb7ae32e..de05ff53 100644 --- a/src/DfReader.cpp +++ b/src/DfReader.cpp @@ -183,6 +183,17 @@ class DfReader { } } + void setTimestamp(time_t c_time, time_t m_time) { + output_.attr("creation_timestamp") = c_time; + output_.attr("modified_timestamp") = m_time; + } + + void setFileEncoding(const char *file_encoding) { + if (file_encoding != NULL && strcmp(file_encoding, "") != 0) { + output_.attr("encoding") = file_encoding; + } + } + void setNote(int note_index, const char *note) { if (note != NULL && strcmp(note, "") != 0) { notes_.push_back(note); @@ -469,6 +480,8 @@ int dfreader_metadata(readstat_metadata_t *metadata, void *ctx) { readstat_get_var_count(metadata) ); ((DfReader*) ctx)->setMetadata(readstat_get_file_label(metadata)); + ((DfReader*) ctx)->setFileEncoding(readstat_get_file_encoding(metadata)); + ((DfReader*) ctx)->setTimestamp(readstat_get_creation_time(metadata), readstat_get_modified_time(metadata)); return 0; } diff --git a/tests/testthat/test-haven-sas.R b/tests/testthat/test-haven-sas.R index b3845fff..ca6cab36 100644 --- a/tests/testthat/test-haven-sas.R +++ b/tests/testthat/test-haven-sas.R @@ -217,6 +217,9 @@ test_that("can roundtrip format attribute", { e = structure(100.12345, format.sas = "COMMA10.3") ) + attr(df, "creation_timestamp") <- as.integer(Sys.time()) + attr(df, "modified_timestamp") <- as.integer(Sys.time()) + path <- tempfile() write_xpt(df, path) diff --git a/tests/testthat/test-haven-spss.R b/tests/testthat/test-haven-spss.R index b5041302..5da7e1e9 100644 --- a/tests/testthat/test-haven-spss.R +++ b/tests/testthat/test-haven-spss.R @@ -420,6 +420,9 @@ test_that("works with empty factors", { test_that("all compression types roundtrip successfully", { df <- tibble::tibble(x = 1:10) + attr(df, "encoding") <- "UTF-8" + attr(df, "creation_timestamp") <- as.integer(Sys.time()) + attr(df, "modified_timestamp") <- as.integer(Sys.time()) expect_equal(roundtrip_sav(df, compress = "byte"), df) expect_equal(roundtrip_sav(df, compress = "none"), df) expect_equal(roundtrip_sav(df, compress = "zsav"), df) diff --git a/tests/testthat/test-haven-stata.R b/tests/testthat/test-haven-stata.R index b81e9317..6a6f5f37 100644 --- a/tests/testthat/test-haven-stata.R +++ b/tests/testthat/test-haven-stata.R @@ -202,11 +202,17 @@ test_that("supports stata version 15", { df <- tibble(x = factor(letters), y = runif(26)) path <- tempfile() + # counld not estimate how long this would take write_dta(df, path, version = 15) df2 <- read_dta(path) + # cheating for correct creation date/modified date + attr(df, "creation_timestamp") <- attributes(df2)$creation_timestamp + attr(df, "modified_timestamp") <- attributes(df2)$modified_timestamp + df2$x <- as_factor(df2$x) df2$y <- zap_formats(df2$y) + expect_equal(df2, df) })