R Under development (unstable) (2025-04-17 r88153 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(datapackage) > source("helpers.R") > > > fielddescriptor <- list( + name = "date", + title = "A date field", + description = "A description", + type = "date" + ) > res <- dp_to_date(c("2020-01-01", "2022-12-31", "", NA), fielddescriptor = fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-31", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > expect_error( + dp_to_date(c("20200101", "20221231", "", NA), fielddescriptor = fielddescriptor) + ) > > fielddescriptor <- list( + name = "date", + title = "A date field", + description = "A description", + type = "date", + format = "default" + ) > res <- dp_to_date(c("2020-01-01", "2022-12-31", "", NA), fielddescriptor = fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-31", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > expect_error( + dp_to_date(c("20200101", "20221231", "", NA), fielddescriptor = fielddescriptor) + ) > > fielddescriptor <- list( + name = "date", + title = "A date field", + description = "A description", + type = "date", + format = "any" + ) > res <- dp_to_date(c("2020-01-01", "2022-12-31", "", NA), fielddescriptor = fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-31", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > expect_error( + dp_to_date(c("20200101", "20221231", "", NA), fielddescriptor = fielddescriptor) + ) > > fielddescriptor <- list( + name = "date", + title = "A date field", + description = "A description", + type = "date", + format = "%Y%m%d" + ) > res <- dp_to_date(c("20200101", "20221231", "", NA), fielddescriptor = fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-31", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > expect_error( + dp_to_date(c("2020-01-01", "2022-12-31", "", NA), fielddescriptor = fielddescriptor) + ) > > > # === No Schema > fielddescriptor <- list( + type = "date" + ) > res <- dp_to_date(c("2020-01-01", "2022-12-31", "", NA)) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-31", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > expect_error( + dp_to_date(c("20200101", "20221231", "", NA), fielddescriptor = fielddescriptor) + ) > > # === NA > fielddescriptor <- list( + name = "date", + missingValues = c("--") + ) > res <- dp_to_date(c("2020-01-01","--", "2022-12-31", NA), fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", NA, "2022-12-31", NA)), attributes = FALSE) > expect_error(res <- dp_to_date(c("2020-01-01","---", "2022-12-31", NA), fielddescriptor)) > > # ============================================================================= > # csv_colclass > > res <- datapackage:::csv_colclass_date(list()) > expect_equal(res, "character") > > res <- datapackage:::csv_colclass_date(list(missingValues = "--")) > expect_equal(res, "character") > > # ============================================================================= > # csv_format > > res <- datapackage:::csv_format_date( + as.Date(c("2020-01-01", "2022-12-21", NA, NA))) > expect_equal(res, c("2020-01-01", "2022-12-21", NA, NA)) > > fielddescriptor <- list( + type = "date", + format = "%Y%m%d" + ) > res <- datapackage:::csv_format_date( + as.Date(c("2020-01-01", "2022-12-21", NA, NA)), + fielddescriptor = fielddescriptor) > expect_equal(res, c("20200101", "20221221", NA, NA)) > > fielddescriptor <- list( + type = "date", + format = "any" + ) > res <- datapackage:::csv_format_date( + as.Date(c("2020-01-01", "2022-12-21", NA, NA)), + fielddescriptor = fielddescriptor) > expect_equal(res, c("2020-01-01", "2022-12-21", NA, NA)) > > fielddescriptor <- list( + type = "date", + format = "default", + missingValues = c("--") + ) > res <- datapackage:::csv_format_date( + as.Date(c("2020-01-01", "2022-12-21", NA, NA)), + fielddescriptor = fielddescriptor) > expect_equal(res, c("2020-01-01", "2022-12-21", "--", "--")) > > > > proc.time() user system elapsed 0.15 0.04 0.18