diff --git a/r/NEWS.md b/r/NEWS.md index a7d36e0fd51..e69d46275fe 100644 --- a/r/NEWS.md +++ b/r/NEWS.md @@ -19,6 +19,9 @@ # arrow 7.0.0.9000 +* `lubridate`: + * `tz()` to extract/get timezone + # arrow 7.0.0 ## Enhancements to dplyr and datasets diff --git a/r/R/dplyr-funcs-datetime.R b/r/R/dplyr-funcs-datetime.R index 04c0214fdfb..d9bfcbd7e3e 100644 --- a/r/R/dplyr-funcs-datetime.R +++ b/r/R/dplyr-funcs-datetime.R @@ -147,5 +147,11 @@ register_bindings_datetime <- function() { register_binding("pm", function(x) { !call_binding("am", x) }) + register_binding("tz", function(x) { + if (!call_binding("is.POSIXct", x)) { + abort(paste0("timezone extraction for objects of class `", type(x)$ToString(), "` not supported in Arrow")) + } + x$type()$timezone() + }) } diff --git a/r/R/type.R b/r/R/type.R index f8b408e4065..0bcd3e77d91 100644 --- a/r/R/type.R +++ b/r/R/type.R @@ -78,7 +78,7 @@ type.default <- function(x) Array__infer_type(x) type.ArrowDatum <- function(x) x$type #' @export -type.Expression <- function(x) x$type +type.Expression <- function(x) x$type() #----- metadata diff --git a/r/tests/testthat/test-dplyr-funcs-datetime.R b/r/tests/testthat/test-dplyr-funcs-datetime.R index a7a705678c1..e3092f3455c 100644 --- a/r/tests/testthat/test-dplyr-funcs-datetime.R +++ b/r/tests/testthat/test-dplyr-funcs-datetime.R @@ -711,3 +711,41 @@ test_that("am/pm mirror lubridate", { ) }) + +test_that("extract tz", { + df <- tibble( + posixct_date = as.POSIXct(c("2022-02-07", "2022-02-10"), tz = "Pacific/Marquesas"), + ) + + compare_dplyr_binding( + .input %>% + mutate(timezone_posixct_date = tz(posixct_date)) %>% + collect(), + df + ) + + # test a few types directly from R objects + expect_error( + call_binding("tz", "2020-10-01"), + "timezone extraction for objects of class `string` not supported in Arrow" + ) + expect_error( + call_binding("tz", as.Date("2020-10-01")), + "timezone extraction for objects of class `date32[day]` not supported in Arrow", + fixed = TRUE + ) + expect_error( + call_binding("tz", 1L), + "timezone extraction for objects of class `int32` not supported in Arrow" + ) + expect_error( + call_binding("tz", 1.1), + "timezone extraction for objects of class `double` not supported in Arrow" + ) + + # Test one expression + expect_error( + call_binding("tz", Expression$scalar("2020-10-01")), + "timezone extraction for objects of class `string` not supported in Arrow" + ) +}) diff --git a/r/tests/testthat/test-type.R b/r/tests/testthat/test-type.R index 168cdcff690..71c2302116a 100644 --- a/r/tests/testthat/test-type.R +++ b/r/tests/testthat/test-type.R @@ -235,7 +235,10 @@ test_that("type() gets the right type for Expression", { y <- Expression$scalar(10) add_xy <- Expression$create("add", x, y) - expect_equal(x$type, type(x)) - expect_equal(y$type, type(y)) - expect_equal(add_xy$type, type(add_xy)) + expect_equal(x$type(), type(x)) + expect_equal(type(x), int32()) + expect_equal(y$type(), type(y)) + expect_equal(type(y), float64()) + expect_equal(add_xy$type(), type(add_xy)) + expect_equal(type(add_xy), float64()) })