diff --git a/r/R/dplyr-funcs-datetime.R b/r/R/dplyr-funcs-datetime.R index 04c0214fdfb..234f0bda872 100644 --- a/r/R/dplyr-funcs-datetime.R +++ b/r/R/dplyr-funcs-datetime.R @@ -148,4 +148,28 @@ register_bindings_datetime <- function() { !call_binding("am", x) }) + register_binding("ymd", function(x) { + format_map <- + list( + ymd_hyphen1 = "%Y-%m-%d", + ymd_hyphen2 = "%y-%m-%d", + ymd_hyphen3 = "%Y-%B-%d", + ymd_hyphen4 = "%y-%B-%d", + ymd_hyphen5 = "%Y-%b-%d", + ymd_hyphen6 = "%y-%b-%d" + ) + + x <- call_binding("gsub", "[^A-Za-z0-9]", "-", x) + + # call_binding( + # "coalesce", + call_binding("strptime", x, format = format_map[[1]], unit = "s")#, + # call_binding("strptime", x, format = format_map[[2]], unit = "s"), + # call_binding("strptime", x, format = format_map[[3]], unit = "s"), + # call_binding("strptime", x, format = format_map[[4]], unit = "s"), + # call_binding("strptime", x, format = format_map[[5]], unit = "s"), + # call_binding("strptime", x, format = format_map[[6]], unit = "s") + # ) + + }) } diff --git a/r/tests/testthat/test-dplyr-funcs-datetime.R b/r/tests/testthat/test-dplyr-funcs-datetime.R index a7a705678c1..549c51dc357 100644 --- a/r/tests/testthat/test-dplyr-funcs-datetime.R +++ b/r/tests/testthat/test-dplyr-funcs-datetime.R @@ -711,3 +711,56 @@ test_that("am/pm mirror lubridate", { ) }) + +test_that("date/time parsing / ymd() and `-` separator", { + test_dates <- tibble::tibble( + string_ymd = c( + "2021-09-10", "2021/09/10", "2021.09.10", "2021,09,10", "2021:09:10", "2021 09 10", + # "21-09-10", + # "21/09/10", + # "21.09.10", + # "21,09,10", + # "21:09:10", + # "20210910", + # "210910", + # "2021 Sep 10", + # "2021 September 10", + # "21 Sep 10", + # "21 September 10", + NA + ), + string_dmy = c( + "10-09-2021", "10/09/2021", "10.09.2021", "10,09,2021", "10:09:2021", "10 09 2021", + # "10-09-21", "10/09/21", + # "10.09.21", "10,09,21", "10:09:21", + # "10092021", + # "100921", + # "10 Sep 2021", + # "10 September 2021", + # "10 Sep 21", + # "10 September 21", + NA + ), + string_mdy = c( + "09-10-2021", "09/10/2021", "09.10.2021", "09,10,2021", "09:10:2021", "09 10 2021", + # "09-10-21", "09/10/21", + # "09.10.21", "09,10,21", "09:10:21", + # "09102021", + # "091021", + # "Sep 10 2021", + # "September 10 2021", + # "Sep 10 21", + # "September 10 21", + NA + ), + date = c(rep(as.Date("2021-09-10"), 6), NA), + date_midnight = c(rep(as.POSIXct("2021-09-10 00:00:00", tz = "UTC"), 6), NA) + ) + + compare_dplyr_binding( + .input %>% + mutate(x = ymd(string_ymd)) %>% + collect(), + test_dates + ) +})