This is an automated email from the ASF dual-hosted git repository.

thisisnic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 1fc9a29  ARROW-14823: [R] Implement bindings for lubridate::leap_year
1fc9a29 is described below

commit 1fc9a2982d5876b84c4cf64d557054480a65d0c9
Author: Danielle Navarro <[email protected]>
AuthorDate: Tue Jan 18 01:31:30 2022 +0000

    ARROW-14823: [R] Implement bindings for lubridate::leap_year
    
    Does what it says in the title: adds binding for `lubridate::leap_year`
    
    Closes #12173 from djnavarro/ARROW-14823
    
    Authored-by: Danielle Navarro <[email protected]>
    Signed-off-by: Nic Crane <[email protected]>
---
 r/R/dplyr-funcs-datetime.R                   |  5 +++++
 r/tests/testthat/test-dplyr-funcs-datetime.R | 32 ++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/r/R/dplyr-funcs-datetime.R b/r/R/dplyr-funcs-datetime.R
index 0ff4294..15a3899 100644
--- a/r/R/dplyr-funcs-datetime.R
+++ b/r/R/dplyr-funcs-datetime.R
@@ -134,4 +134,9 @@ register_bindings_datetime <- function() {
     inherits(x, "POSIXct") ||
       (inherits(x, "Expression") && x$type_id() %in% Type[c("TIMESTAMP")])
   })
+
+  register_binding("leap_year", function(date) {
+    year <- Expression$create("year", date)
+    (year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0))
+  })
 }
diff --git a/r/tests/testthat/test-dplyr-funcs-datetime.R 
b/r/tests/testthat/test-dplyr-funcs-datetime.R
index c3f79dc..359a540 100644
--- a/r/tests/testthat/test-dplyr-funcs-datetime.R
+++ b/r/tests/testthat/test-dplyr-funcs-datetime.R
@@ -634,3 +634,35 @@ test_that("extract yday from date", {
     test_df
   )
 })
+
+test_that("leap_year mirror lubridate", {
+
+  compare_dplyr_binding(
+    .input %>%
+      mutate(x = leap_year(date)) %>%
+      collect(),
+    test_df
+  )
+
+  compare_dplyr_binding(
+    .input %>%
+      mutate(x = leap_year(datetime)) %>%
+      collect(),
+    test_df
+  )
+
+  compare_dplyr_binding(
+    .input %>%
+      mutate(x = leap_year(test_year)) %>%
+      collect(),
+    data.frame(
+      test_year = as.Date(c(
+        "1998-01-01", # not leap year
+        "1996-01-01", # leap year (divide by 4 rule)
+        "1900-01-01", # not leap year (divide by 100 rule)
+        "2000-01-01"  # leap year (divide by 400 rule)
+      ))
+    )
+  )
+
+})

Reply via email to