R Under development (unstable) (2025-03-09 r87914 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") > > # === DEFAULT > fielddescriptor <- list( + name = "year", + title = "A year field", + description = "A description", + type = "year" + ) > res <- dp_to_year(c("2020", "-4", "", NA), + fielddescriptor = fielddescriptor) > expect_equal(res, c(2020, -4, NA, NA), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > suppressWarnings(expect_error( + to_datetime(c("2022.25", "", NA), fielddescriptor = fielddescriptor) + )) > > # === NO FIELDDESCRIPTOR > fielddescriptor <- list( + type = "year" + ) > res <- dp_to_year(c("2020", "-4", "", NA)) > expect_equal(res, c(2020, -4, NA, NA), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > suppressWarnings(expect_error( + to_datetime(c("2022.25", "", NA), fielddescriptor = fielddescriptor) + )) > > # === MISSINGVALUES > fielddescriptor <- list( + name = "year", + title = "A year field", + description = "A description", + type = "year", + missingValues = c("0", "-") + ) > res <- dp_to_year(c("2020", "-4", "-", NA), + fielddescriptor = fielddescriptor) > expect_equal(res, c(2020, -4, NA, NA), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > suppressWarnings(expect_error( + to_datetime(c("2022.25", "", NA), fielddescriptor = fielddescriptor) + )) > > > # === NUMERIC > res <- dp_to_year(1970) > expect_equal(res, 1970L, attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "year")) > > > # === DATE > res <- dp_to_year(as.Date("1980-01-06")) > expect_equal(res, 1980L, attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "year")) > > # === TIME > res <- dp_to_year(as.POSIXct("1980-01-06 14:15")) > expect_equal(res, 1980L, attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "year")) > res <- dp_to_year(as.POSIXlt("1980-01-06 14:15")) > expect_equal(res, 1980L, attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "year")) > > > # ============================================================================= > # csv_colclass > res <- datapackage:::csv_colclass_year() > expect_equal(res, "integer") > > res <- datapackage:::csv_colclass(list(type = "year")) > expect_equal(res, "integer") > > res <- datapackage:::csv_colclass(list(type = "year", missingValues = "X")) > expect_equal(res, "character") > > > > # ============================================================================= > # csv_format > > res <- datapackage:::csv_format_year(c(2020, NA)) > expect_equal(res, c(2020, NA)) > > res <- datapackage:::csv_format_year(c(2020, NA), + fielddescriptor = list(missingValues = c("X", "0"))) > expect_equal(res, c("2020", "X")) > > > > proc.time() user system elapsed 0.14 0.10 0.18