You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/21 20:25:59 UTC

[GitHub] [arrow] jonkeane commented on a change in pull request #12506: ARROW-15098 [R] Add binding for `lubridate::duration()` and/or `as.difftime()`

jonkeane commented on a change in pull request #12506:
URL: https://github.com/apache/arrow/pull/12506#discussion_r831513381



##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -189,6 +189,65 @@ register_bindings_datetime <- function() {
   })
 }
 
+register_bindings_duration <- function() {
+  register_binding("difftime", function(time1,
+                                        time2,
+                                        tz,
+                                        units = c("auto", "secs", "mins",
+                                                  "hours", "days", "weeks")) {
+    units <- match.arg(units)
+    if (units != "secs") {
+      abort("`difftime()` with units other than seconds not supported in Arrow")
+    }
+
+    if (!missing(tz)) {
+      warn("`tz` is an optional argument to `difftime()` in R and will not be used in Arrow")
+    }
+
+    time1 <- build_expr("cast", time1, options = cast_options(to_type = timestamp()))
+    time2 <- build_expr("cast", time2, options = cast_options(to_type = timestamp()))
+
+    build_expr("cast", time1 - time2, options = cast_options(to_type = duration("s")))
+  })
+
+  register_binding("as.difftime", function(x,
+                                           format = "%X",
+                                           units = "auto") {
+    # windows doesn't seem to like "%X"
+    if (format == "%X" & tolower(Sys.info()[["sysname"]]) == "windows") {
+      format <- "%H:%M:%S"
+    }
+
+    if (units != "secs") {
+      abort("`as.difftime()` with units other than seconds not supported in Arrow")
+    }
+
+    if (call_binding("is.character", x)) {
+      x <- build_expr("strptime", x, options = list(format = format, unit = 0L))
+      y <- build_expr("strptime", "0:0:0", options = list(format = "%H:%M:%S", unit = 0L))
+      diff_x_y <- call_binding("difftime", x, y, units = "secs")
+      return(diff_x_y)
+    }

Review comment:
       Do we have a jira for allowing them to? They probably should be able to be cast to `duration`s




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org