djnavarro commented on code in PR #12154:
URL: https://github.com/apache/arrow/pull/12154#discussion_r843498828
##########
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:
While I'm here, @rok, I thought I might flag another potential problem with
handling the `week_start` issue. The approach I've taken, whenever `week_start`
takes on values other than 1 or 7, is to create an "offset" duration that we
can add to the temporal object, round this new temporal object to the nearest
week as if the week started on Monday, and then subtract the offset duration
again. It's inelegant but in principle it should work, except I can't figure
out how to add a scalar duration to an array of temporal objects (e.g., date
32). It's throwing an error saying that `add_checked` doesn't have a kernel for
these data types, which is probably a bit beyond my C++ skills to fix 😁
--
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]