Skip to content
Closed
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
24 changes: 24 additions & 0 deletions r/R/dplyr-funcs-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
# )

})
}
53 changes: 53 additions & 0 deletions r/tests/testthat/test-dplyr-funcs-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have a check that if the format fails, we get NAs out — so long as that's possible)

It looks like right now, it throws an error for the whole conversion: https://github.com/apache/arrow/runs/5094263843?check_suite_focus=true#step:6:22119

})