date.infer <- function (x.call, quiet) {


.charToDate <- function(char, punct, quiet) {

  txt <- character(length=4)
  txt[1] <- "\nTo see all possible formats, enter: ?strptime\n"
  txt[2] <- "Examples:  \"08/18/2024\" format is \"%m/%d/%Y\"\n"
  txt[3] <- "           \"18-08-24\"   format is \"%d-%m-%y\"\n"
  txt[4] <- "           \"August 18, 2024\" format is \"%B %d, %Y\"\n\n"

  dc <- strsplit(char, punct)  # Extract dc, date components
  c1 <- as.numeric(sapply(dc, function(x) x[1]))
  c2 <- as.numeric(sapply(dc, function(x) x[2]))
  c3 <- as.numeric(sapply(dc, function(x) x[3]))
  mx1 <- max(c1);  mx2 <- max(c2);  mx3 <- max(c3)
  unq1 <- length(unique(c1)); unq2 <- length(unique(c2))

  if (!is.na(mx1) && !is.na(mx2)) {  # non-numeric chars entered for a date
    fmt <- NULL

    # date format is correctly inferred if proper dates
    if (mx1 > 31)
      fmt <- paste("%Y", punct, "%m", punct, "%d", sep="")
    else if (mx1 > 12)
      fmt <- paste("%d", punct, "%m", punct, "%Y", sep="")
    else if (mx2 > 12)
      fmt <- paste("%m", punct, "%d", punct, "%Y", sep="")

    # guess at date format
    else if (unq2<=12 && (unq2 %in% c(2,4,12)))
      fmt <- paste("%d", punct, "%m", punct, "%Y", sep="")
    else if (unq1<=12 && (unq1 %in% c(2,4,12)))
      fmt <- paste("%m", punct, "%d", punct, "%Y", sep="")

    # allow for 2-digit year, is positioned  at 3rd component
    if (!is.null(fmt)) {
      if (substr(fmt,2,2) != "Y") {
        if (mx3 <= 99) fmt <- sub("Y", "y", fmt, fixed=TRUE)
       }
    }

    # obtain only valid dates
    dates <- try(as.Date(char, format=fmt), silent=FALSE)

    # process valid dates further to obtain the likely correct valid date
    if (!inherits(dates, "try-error") && all(!is.na(dates))) {  # no errors
        if (!quiet) {
          message("\nBest guess for the date format: ", fmt, "\n")
          message("Otherwise, specify with parameter: ts_format", txt, "\n")
        }
        return(dates)  # Return converted dates
    }
  }  # all chars numeric

  else {  # some non-numeric chars in at least one date
    message("\n"); stop(call.=FALSE, "\n------\n",
      "At least one date contains non-numeric characters\n",
      "  where there should be a number.\n")
  }

  # date not recognized
  # only way to get here is if date did not work
  message("\n"); stop(call.=FALSE, "\n------\n",
    "The date format could not be properly inferred.\n\n",
    "Specify the date format with:  ts_format", txt, "\n")
}  # end .charToDate


# BEGIN -------------------------------------------------------------------

  x11 <- x.call[1,1]

  n.ch <- nchar(x11)
  if (n.ch %in% 6:10) {
    isQ <- grepl("Q1|Q2|Q3|Q4", x11)
    isM <- grepl("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", x11)
    if (isM) {
      x.call[,1] <- gsub(" ", "", x.call[,1])  # remove all spaces
      year <- substr(x.call[,1], 1, 4)
      monthNm <- substr(x.call[,1], 5, 7)
      x.call[,1] <- as.Date(paste(year, monthNm, "01", sep="-"),
                             format="%Y-%b-%d")
    }
    else if (isQ) {  # convert dates entered as 2024 Q3 to R Date
      parts <- strsplit(gsub("\\s+", "", x.call[,1]), "Q")  # remove Q
      # Extract quarter, `[` is R extraction operator
      year <- as.numeric(sapply(parts, `[`, 1))  # 1st list element (year)
      quarter <- as.numeric(sapply(parts, `[`, 2))  # 2nd list element
      month <- 1 + (quarter - 1) * 3  # get month Q1=1, Q2=4, Q3=7, Q4=10
      x.call[,1] <- as.Date(paste(year, month, "01", sep="-"))  #to Date
    }  # end quarter
    else {  # see if numeric numeric date format
      punct <- " "  # see if there are two punctuation delimiters
      if (length(gregexpr("/", x11, fixed=TRUE)[[1]]) == 2) punct <- "/"
      if (length(gregexpr("-", x11, fixed=TRUE)[[1]]) == 2) punct <- "-"
      if (length(gregexpr(".", x11, fixed=TRUE)[[1]]) == 2) punct <- "."
      if (punct %in% c("/", "-", "."))   # only evaluate probable dates
        x.call[,1] <- .charToDate(x.call[,1], punct, quiet)
    }  # numeric date format
  }  # end n.ch is 6, 7, 8

  return(x.call)
}
