dragosmg commented on a change in pull request #12506:
URL: https://github.com/apache/arrow/pull/12506#discussion_r834079820
##########
File path: r/R/dplyr-funcs-datetime.R
##########
@@ -236,6 +239,69 @@ register_bindings_datetime <- function() {
tz = "UTC") {
call_binding("make_datetime", year, month, day, hour, min, sec, tz)
})
+ register_binding("difftime", function(time1,
+ time2,
+ tz,
+ units = "secs") {
+ if (units != "secs") {
+ abort("`difftime()` with units other than `secs` not supported in Arrow")
+ }
+
+ if (!missing(tz)) {
+ warn("`tz` argument is not supported in Arrow, so it will be ignored")
+ }
+
+ # cast to timestamp if time1 and time2 are not dates or timestamp
expressions
+ # (the subtraction of which would output a `duration`)
+ if (!call_binding("is.instant", time1)) {
+ time1 <- build_expr("cast", time1, options = cast_options(to_type =
timestamp(timezone = "UTC")))
+ }
+
+ if (!call_binding("is.instant", time2)) {
+ time2 <- build_expr("cast", time2, options = cast_options(to_type =
timestamp(timezone = "UTC")))
+ }
+
+ # we need to go build the subtract expression instead of `time1 - time2` to
+ # prevent complaints when we try to subtract an R object from an Expression
+ subtract_output <- build_expr("-", time1, time2)
+ build_expr("cast", subtract_output, options = cast_options(to_type =
duration("s")))
+ })
+ register_binding("as.difftime", function(x,
+ format = "%X",
+ units = "secs") {
+ # 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 'secs' not supported in
Arrow")
+ }
+
+ if (call_binding("is.character", x)) {
+ x <- build_expr("strptime", x, options = list(format = format, unit =
0L))
+ # Complex casting only due to cast type restrictions: time64 -> int64 ->
duration(us) -> duration(s)
+ x <-
x$cast(time64("us"))$cast(int64())$cast(duration("us"))$cast(duration("s"))
+ return(x)
+ }
+
+ # 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
+ # if we abort for all doubles, we risk erroring in cases in which
+ # coercion to int64() would work
+ x <- build_expr("cast", x, options = cast_options(to_type = int64()))
+ x <- build_expr("cast", x, options = cast_options(to_type =
duration("s")))
+ return(x)
Review comment:
Done
--
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]