jonkeane commented on code in PR #46206:
URL: https://github.com/apache/arrow/pull/46206#discussion_r2082759048


##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -826,3 +827,54 @@ register_bindings_datetime_rounding <- function() {
     }
   )
 }
+
+register_bindings_hms <- function() {
+  numeric_to_time32 <- function(x) {
+    # The only numeric which can be cast to time32 is int32 so double cast to 
make sure
+    cast(cast(x, int32()), time32(unit = "s"))
+  }
+
+  register_binding(
+    "hms::hms",
+    function(seconds = 0, minutes = 0, hours = 0, days = 0) {
+      total_secs <- seconds +
+        Expression$create("multiply_checked", minutes, 60) +
+        Expression$create("multiply_checked", hours, 3600) +
+        Expression$create("multiply_checked", days, 86400)
+
+      return(numeric_to_time32(total_secs))
+    }
+  )
+
+  register_binding(
+    "hms::as_hms",
+    function(x = numeric()) {
+      datetime_to_time32 <- function(datetime) {
+        hour <- call_binding("hour", datetime)
+        min <- call_binding("minute", datetime)
+        sec <- call_binding("second", datetime)
+
+        return(call_binding("hms::hms", seconds = sec, minutes = min, hours = 
hour))
+      }
+
+      if (call_binding("is.POSIXct", x)) {
+        return(datetime_to_time32(x))
+      }
+
+      if (call_binding("is.numeric", x)) {
+        return(numeric_to_time32(x))
+      }
+
+      if (call_binding("is.character", x)) {
+        dash <- call_binding("gsub", ":", "-", x)
+        as_date_time_string <- call_binding("str_c", "1970-01-01", dash, sep = 
"-")
+        as_date_time <- Expression$create(
+          "strptime",
+          as_date_time_string,
+          options = list(format = "%Y-%m-%d-%H-%M-%S", unit = 0L)
+        )
+        return(datetime_to_time32(as_date_time))
+      }

Review Comment:
   Oh, that's probably fine, honestly. The `hms` error there _is a fairly low 
bar_ but I actually think the arrow one is slighty nicer 😂 



-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to