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


##########
r/tests/testthat/test-dplyr-funcs-datetime.R:
##########
@@ -150,6 +150,71 @@ test_that("strptime", {
     as.POSIXct(tstamp),
     ignore_attr = "tzone"
   )
+
+  tz <- "Pacific/Marquesas"
+  set.seed(42)
+  times <- seq(as.POSIXct("1999-02-07", tz = tz), as.POSIXct("2000-01-01", tz 
= tz), by = "sec")
+  times <- sample(times, 100)
+
+  # %Op format is currently not supported by strptime
+  # We support a subset of flags on Windows
+  if (tolower(Sys.info()[["sysname"]]) == "windows") {
+    formats <- c(
+      "%d", "%H", "%j", "%m", "%T", "%S", "%q", "%M", "%U", "%w", "%W", "%y", 
"%Y", "%R"
+    )
+    formats2 <- c("d", "H", "j", "m", "T", "S", "q", "M", "U", "w", "W", "y", 
"Y", "R")
+  } else {
+    formats <- c(
+      "%a", "%A", "%b", "%B", "%d", "%H", "%j", "%m", "%Om", "%T", "%OS", 
"%I%p",
+      "%S", "%q", "%M", "%U", "%w", "%W", "%y", "%Y", "%r", "%R", "%T%z"
+    )
+    formats2 <- c(
+      "a", "A", "b", "B", "d", "H", "j", "m", "Om", "T", "OS", "Ip",
+      "S", "q", "M", "U", "w", "W", "y", "Y", "r", "R", "Tz"
+    )
+  }
+
+  base_format <- c("%Y-%m-%d")
+  base_format2 <- c("ymd")

Review Comment:
   ```suggestion
     base_format <- "%Y-%m-%d"
     base_format2 <- "ymd"
   ```



##########
r/R/dplyr-datetime-helpers.R:
##########
@@ -171,61 +171,35 @@ build_formats <- function(orders) {
   # 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", "yOm", "Omy")
+  quarter_orders <- c("yq", "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 == "qy")) {
-    orders1 <- setdiff(orders, "qy")
-    orders2 <- "ymd"
-    orders <- unique(c(orders1, orders2))
+  if (any(orders %in% quarter_orders)) {
+    orders <- c(setdiff(orders, quarter_orders), "ymd")
   }
+  orders <- unique(orders)
 
-  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)

Review Comment:
   ```suggestion
     formats_length <- map_int(map_int(formats_list, nchar), max)
   ```
   
   ...is almost certainly what you mean here?



##########
r/R/dplyr-datetime-helpers.R:
##########
@@ -240,25 +214,24 @@ build_formats <- function(orders) {
 #' @noRd
 build_format_from_order <- function(order) {
   char_list <- list(
-    "y" = c("%y", "%Y"),
-    "m" = c("%m", "%B", "%b"),
-    "d" = "%d",
-    "H" = "%H",
-    "M" = "%M",
-    "S" = "%S",
-    "I" = "%I"
+    "%T" = "%H-%M-%S",
+    "%y" = c("%y", "%Y"),
+    "%m" = c("%m", "%B", "%b"),
+    "%b" = c("%m", "%B", "%b")
   )
 
-  split_order <- strsplit(order, split = "")[[1]]
+  formats <- regmatches(order, gregexpr("(O{0,1}[a-zA-Z])", order))[[1]]
+  formats <- paste0("%", formats)
+  formats <- ifelse(formats %in% names(char_list), char_list[formats], formats)
 
-  outcome <- expand.grid(char_list[split_order])
+  outcome <- expand.grid(formats)
   # we combine formats with and without the "-" separator, we will later
   # coalesce through all of them (benchmarking indicated this is a more
   # computationally efficient approach rather than figuring out if a string has
   # separators or not and applying only )

Review Comment:
   It might be nice to complete this sentence while you're here



##########
r/R/dplyr-datetime-helpers.R:
##########
@@ -171,61 +171,35 @@ build_formats <- function(orders) {
   # 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", "yOm", "Omy")
+  quarter_orders <- c("yq", "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 == "qy")) {
-    orders1 <- setdiff(orders, "qy")
-    orders2 <- "ymd"
-    orders <- unique(c(orders1, orders2))
+  if (any(orders %in% quarter_orders)) {
+    orders <- c(setdiff(orders, quarter_orders), "ymd")
   }
+  orders <- unique(orders)
 
-  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

Review Comment:
   It seems unlikely that < 6 characters is the *only* threshold for an invalid 
order?



##########
r/R/dplyr-datetime-helpers.R:
##########
@@ -240,25 +214,24 @@ build_formats <- function(orders) {
 #' @noRd
 build_format_from_order <- function(order) {
   char_list <- list(
-    "y" = c("%y", "%Y"),
-    "m" = c("%m", "%B", "%b"),
-    "d" = "%d",
-    "H" = "%H",
-    "M" = "%M",
-    "S" = "%S",
-    "I" = "%I"
+    "%T" = "%H-%M-%S",
+    "%y" = c("%y", "%Y"),
+    "%m" = c("%m", "%B", "%b"),
+    "%b" = c("%m", "%B", "%b")

Review Comment:
   ```suggestion
       "%y" = "%y", "%Y",
       "%m" = "%m", "%B", "%b",
       "%b" = "%m", "%B", "%b"
   ```



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