djnavarro commented on code in PR #12154:
URL: https://github.com/apache/arrow/pull/12154#discussion_r844495246


##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -166,6 +166,73 @@ register_bindings_datetime <- function() {
       (inherits(x, "Expression") && x$type_id() %in% Type[c("TIMESTAMP")])
   })
 
+  register_binding("round_date", function(x, unit = "second",
+                                          week_start = 
getOption("lubridate.week.start", 7)) {
+    opts <- parse_period_unit(unit)
+    if (opts$unit == 7L) {
+      if (week_start == 7) { # Sunday
+        opts$week_starts_monday <- 0L
+        return(Expression$create("round_temporal", x, options = opts))
+
+      } else if (week_start == 1) { # Monday
+        opts$week_starts_monday <- 1L
+        return(Expression$create("round_temporal", x, options = opts))
+
+      } else { # other values of week_start
+
+        # create a duration object as an offset
+        shift <- build_expr(
+          "cast",
+          Scalar$create((as.integer(week_start) - 1L) * 86400L, int64()),
+          options = cast_options(to_type = duration(unit = "s"))
+        )
+
+        # add the offset, round, then subtract the offset
+        # [throws error: add_checked has no kernel for date32-array and 
duration(s)-scalar]
+        interim <- build_expr("round_temporal", x + shift, options = opts)
+        return(interim - shift)

Review Comment:
   Ah! The kernels are there for durations encoded in more precise units, so 
when I encode the shift in milliseconds it no longer throws that error:
   
   ```r
           shift <- build_expr(
             "cast",
             Scalar$create((as.integer(week_start) - 1L) * 86400000L, int64()),
             options = cast_options(to_type = duration(unit = "ms"))
           )
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to