R Under development (unstable) (2025-05-27 r88249 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") > > # === NO FORMAT > fielddescriptor <- list( + name = "yearmonth", + title = "A yearmonth field", + description = "A description", + type = "yearmonth" + ) > res <- dp_to_yearmonth(c("2020-01", "202212", "", NA), + fielddescriptor = fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-01", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > suppressWarnings(expect_error( + to_datetime(c("2022 01", "2024 01", "", NA), fielddescriptor = fielddescriptor) + )) > > # === NO FIELDDESCRIPTOR > fielddescriptor <- list( + type = "yearmonth" + ) > res <- dp_to_yearmonth(c("2020-01", "202212", "", NA)) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-01", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > suppressWarnings(expect_error( + to_datetime(c("2022 01", "2024 01", "", NA), fielddescriptor = fielddescriptor) + )) > > # === MISSINGVALUES > fielddescriptor <- list( + name = "yearmonth", + title = "A yearmonth field", + description = "A description", + type = "yearmonth", + missingValues = "0000-00" + ) > res <- dp_to_yearmonth(c("2020-01", "202212", "0000-00", NA), + fielddescriptor = fielddescriptor) > expect_equal(res, as.Date(c("2020-01-01", "2022-12-01", NA, NA)), attributes = FALSE) > expect_attribute(res, "fielddescriptor", fielddescriptor) > suppressWarnings(expect_error( + res <- dp_to_yearmonth(c("2020-01", "202212", "-------", NA), + fielddescriptor = fielddescriptor) + )) > > > > > # === NUMERIC > res <- dp_to_yearmonth(197001) > expect_equal(res, as.Date("1970-01-01"), attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "yearmonth")) > > > # === DATE > res <- dp_to_yearmonth(as.Date("1980-01-06")) > expect_equal(res, as.Date("1980-01-01"), attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "yearmonth")) > > # === TIME > res <- dp_to_yearmonth(as.POSIXct("1980-01-06 14:15")) > expect_equal(res, as.Date("1980-01-01"), attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "yearmonth")) > res <- dp_to_yearmonth(as.POSIXlt("1980-01-06 14:15")) > expect_equal(res, as.Date("1980-01-01"), attributes = FALSE) > expect_attribute(res, "fielddescriptor", list(type = "yearmonth")) > > > # ============================================================================= > # csv_colclass > res <- datapackage:::csv_colclass_yearmonth() > expect_equal(res, "character") > > res <- datapackage:::csv_colclass(list(type = "yearmonth")) > expect_equal(res, "character") > > > # ============================================================================= > # csv_format > > res <- datapackage:::csv_format_yearmonth(as.Date("2020-01-01")) > expect_equal(res, "2020-01") > > res <- datapackage:::csv_format_yearmonth(as.Date("2020-01-05")) > expect_equal(res, "2020-01") > > fielddescriptor <- list( + type = "yearmonth", + missingValues = c("0000-00", "-------") + ) > res <- datapackage:::csv_format_yearmonth(as.Date(c("2020-01-01", NA)), + fielddescriptor = fielddescriptor) > expect_equal(res, c("2020-01", "0000-00")) > > > proc.time() user system elapsed 0.18 0.17 0.32