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

add creation/modified time/file encoding to attr #733

Open
wants to merge 3 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
13 changes: 13 additions & 0 deletions src/DfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-haven-sas.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-haven-spss.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-haven-stata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down