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



##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -189,6 +189,70 @@ 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")
+    }
+
+    # for time32() we do not need to worry about timezone
+    if (call_binding("is.instant", time1) & call_binding("is.instant", time2)) 
{
+      if (missing(tz)) {
+        time1 <- build_expr("cast", time1, options = cast_options(to_type = 
timestamp(unit = "s")))
+        time2 <- build_expr("cast", time2, options = cast_options(to_type = 
timestamp(unit = "s")))
+      } else {
+        time1 <- build_expr("cast", time1, options = cast_options(to_type = 
timestamp(timezone = tz, unit = "s")))
+        time2 <- build_expr("cast", time2, options = cast_options(to_type = 
timestamp(timezone = tz, unit = "s")))
+      }
+    }
+
+    build_expr("cast", time1 - time2, options = cast_options(to_type = 
duration("s")))
+  })
+
+  register_binding("as.difftime", function(x,
+                                           format = "%X",
+                                           units = "auto",
+                                           tz = "UTC") {
+    # 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, tz = tz, 
unit = 0L))
+      y <- build_expr("strptime", "0:0:0", options = list(format = "%H:%M:%S", 
tz = tz, unit = 0L))
+      diff_x_y <- call_binding("difftime", x, y, units = "secs", tz = tz)
+      return(diff_x_y)
+    }
+
+    # numeric -> duration not supported in Arrow yet so we use int64() as an
+    # intermediate step
+    # TODO revisit if https://issues.apache.org/jira/browse/ARROW-15862 results
+    # in numeric -> duration support
+
+    if (call_binding("is.numeric", x)) {
+      # coerce x to be int64(). it should work for integer-like doubles and 
fail
+      # for pure doubles

Review comment:
       That works - apprently the correct term is "mantissa" but I think your 
made-up term makes more sense here ;)

##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -189,6 +189,70 @@ 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")
+    }
+
+    # for time32() we do not need to worry about timezone
+    if (call_binding("is.instant", time1) & call_binding("is.instant", time2)) 
{
+      if (missing(tz)) {
+        time1 <- build_expr("cast", time1, options = cast_options(to_type = 
timestamp(unit = "s")))
+        time2 <- build_expr("cast", time2, options = cast_options(to_type = 
timestamp(unit = "s")))
+      } else {
+        time1 <- build_expr("cast", time1, options = cast_options(to_type = 
timestamp(timezone = tz, unit = "s")))
+        time2 <- build_expr("cast", time2, options = cast_options(to_type = 
timestamp(timezone = tz, unit = "s")))
+      }
+    }
+
+    build_expr("cast", time1 - time2, options = cast_options(to_type = 
duration("s")))
+  })
+
+  register_binding("as.difftime", function(x,
+                                           format = "%X",
+                                           units = "auto",
+                                           tz = "UTC") {
+    # 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, tz = tz, 
unit = 0L))
+      y <- build_expr("strptime", "0:0:0", options = list(format = "%H:%M:%S", 
tz = tz, unit = 0L))
+      diff_x_y <- call_binding("difftime", x, y, units = "secs", tz = tz)
+      return(diff_x_y)
+    }
+
+    # numeric -> duration not supported in Arrow yet so we use int64() as an
+    # intermediate step
+    # TODO revisit if https://issues.apache.org/jira/browse/ARROW-15862 results
+    # in numeric -> duration support
+
+    if (call_binding("is.numeric", x)) {
+      # coerce x to be int64(). it should work for integer-like doubles and 
fail
+      # for pure doubles

Review comment:
       That works - apparently the correct term is "mantissa" but I think your 
made-up term makes more sense here ;)




-- 
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