dragosmg commented on code in PR #12757:
URL: https://github.com/apache/arrow/pull/12757#discussion_r848467721
##########
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:
Done. I like the `purrr` suggestion (I think the `for` loop is a tiny bit
more readable).
--
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]