From 79970720c30219e6050037baf936c3d0ba170aeb Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 25 Aug 2023 22:47:17 +0800 Subject: [PATCH 1/3] add creation/modified time/file encoding to attr --- src/DfReader.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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; } From 356848dbe05ff9519ed488f4264f7695b8705132 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 9 Sep 2023 14:31:15 +0800 Subject: [PATCH 2/3] update sas/spss/stata tests for new attrs --- tests/testthat/test-haven-sas.R | 3 +++ tests/testthat/test-haven-spss.R | 3 +++ tests/testthat/test-haven-stata.R | 4 ++++ 3 files changed, 10 insertions(+) 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..3bfd8e62 100644 --- a/tests/testthat/test-haven-stata.R +++ b/tests/testthat/test-haven-stata.R @@ -207,6 +207,10 @@ test_that("supports stata version 15", { df2$x <- as_factor(df2$x) df2$y <- zap_formats(df2$y) + + attr(df, "creation_timestamp") <- as.integer(Sys.time()) + attr(df, "modified_timestamp") <- as.integer(Sys.time()) + expect_equal(df2, df) }) From 7f3eef91a851796411892c57dac8d7721016619a Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 9 Sep 2023 14:43:27 +0800 Subject: [PATCH 3/3] update stata tests for timestamp attr --- tests/testthat/test-haven-stata.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-haven-stata.R b/tests/testthat/test-haven-stata.R index 3bfd8e62..6a6f5f37 100644 --- a/tests/testthat/test-haven-stata.R +++ b/tests/testthat/test-haven-stata.R @@ -202,15 +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) - attr(df, "creation_timestamp") <- as.integer(Sys.time()) - attr(df, "modified_timestamp") <- as.integer(Sys.time()) - expect_equal(df2, df) })