rok commented on code in PR #13506:
URL: https://github.com/apache/arrow/pull/13506#discussion_r930223895


##########
r/R/dplyr-datetime-helpers.R:
##########
@@ -158,74 +158,40 @@ binding_as_date_numeric <- function(x, origin = 
"1970-01-01") {
 #'
 #' @noRd
 build_formats <- function(orders) {
-  # only keep the letters and the underscore as separator -> allow the users to
-  # pass strptime-like formats (with "%"). We process the data -> we need to
-  # process the `orders` (even if supplied in the desired format)
-  # Processing is needed (instead of passing
-  # formats as-is) due to the processing of the character vector in 
parse_date_time()
-  orders <- gsub("[^A-Za-z]", "", orders)
-  orders <- gsub("Y", "y", orders)
-
   # we separate "ym', "my", and "yq" from the rest of the `orders` vector and
   # transform them. `ym` and `yq` -> `ymd` & `my` -> `myd`
   # this is needed for 2 reasons:
   # 1. strptime does not parse "2022-05" -> we add "-01", thus changing the 
format,
   # 2. for equivalence to lubridate, which parses `ym` to the first day of the 
month
-  short_orders <- c("ym", "my")
+  short_orders <- c("ym", "my", "Ym", "mY")
+  quarter_orders <- c("yq", "Yq", "qy", "qY")
 
   if (any(orders %in% short_orders)) {
     orders1 <- setdiff(orders, short_orders)
     orders2 <- intersect(orders, short_orders)
     orders2 <- paste0(orders2, "d")
     orders <- unique(c(orders2, orders1))
   }
-
-  if (any(orders == "yq")) {
-    orders1 <- setdiff(orders, "yq")
-    orders2 <- "ymd"
-    orders <- unique(c(orders1, orders2))
+  if (any(orders %in% quarter_orders)) {
+    orders <- c(setdiff(orders, quarter_orders), "ymd")
   }
+  orders <- unique(orders)
 
-  if (any(orders == "qy")) {
-    orders1 <- setdiff(orders, "qy")
-    orders2 <- "ymd"
-    orders <- unique(c(orders1, orders2))
-  }
-
-  ymd_orders <- c("ymd", "ydm", "mdy", "myd", "dmy", "dym")
-  ymd_hms_orders <- c(
-    "ymd_HMS", "ymd_HM", "ymd_H", "dmy_HMS", "dmy_HM", "dmy_H", "mdy_HMS",
-    "mdy_HM", "mdy_H", "ydm_HMS", "ydm_HM", "ydm_H"
-  )
-  # support "%I" hour formats
-  ymd_ims_orders <- gsub("H", "I", ymd_hms_orders)
-
-  supported_orders <- c(
-    ymd_orders,
-    ymd_hms_orders,
-    gsub("_", " ", ymd_hms_orders), # allow "_", " " and "" as order separators
-    gsub("_", "", ymd_hms_orders),
-    ymd_ims_orders,
-    gsub("_", " ", ymd_ims_orders), # allow "_", " " and "" as order separators
-    gsub("_", "", ymd_ims_orders)
-  )
-
-  unsupported_passed_orders <- setdiff(orders, supported_orders)
-  supported_passed_orders <- intersect(orders, supported_orders)
+  formats_list <- map(orders, build_format_from_order)
+  formats_length <- map(map(formats_list, nchar), max)
+  invalid_orders <- formats_length < 6
 
-  # error only if there isn't at least one valid order we can try
-  if (length(supported_passed_orders) == 0) {
+  if (any(invalid_orders)) {
     arrow_not_supported(
       paste0(
         oxford_paste(
-          unsupported_passed_orders
+          orders[invalid_orders]
         ),
         " `orders`"
       )
     )
   }

Review Comment:
   [ARROW-17218](https://issues.apache.org/jira/browse/ARROW-17218).



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