thisisnic commented on code in PR #12757:
URL: https://github.com/apache/arrow/pull/12757#discussion_r848487916


##########
r/R/dplyr-funcs-datetime.R:
##########
@@ -330,3 +349,33 @@ binding_format_datetime <- function(x, format = "", tz = 
"", usetz = FALSE) {
 
   build_expr("strftime", x, options = list(format = format, locale = 
Sys.getlocale("LC_TIME")))
 }
+
+duration_from_chunks <- function(chunks) {
+  accepted_chunks <- c("second", "minute", "hour", "day", "week")
+  matched_chunks <- accepted_chunks[pmatch(names(chunks), accepted_chunks, 
duplicates.ok = TRUE)]
+
+  if (any(is.na(matched_chunks))) {
+    abort(
+      paste0(
+        "Invalid `difftime` parts: ",
+        oxford_paste(names(chunks[is.na(matched_chunks)]), quote_symbol = "`")
+      )
+    )
+  }
+
+  matched_chunks <- matched_chunks[!is.na(matched_chunks)]
+
+  chunks <- chunks[matched_chunks]
+  chunk_duration <- c(
+    "second" = 1L,
+    "minute" = 60L,
+    "hour" = 3600L,
+    "day" = 86400L,
+    "week" = 604800L
+  )
+  duration <- 0L
+  for (chunk in names(chunks)) {
+    duration <- duration + chunks[[chunk]] * chunk_duration[[chunk]]
+  }
+  duration

Review Comment:
   Good point - I agree and actually think the `for` loop is a lot more 
readable actually even if it's not as "idiomatic".  Hmm, if you update it to 
something like my suggestion, perhaps chuck in a comment to explain it?



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