R/nlogotest-misc-utils.R

Defines functions parse_git_date_and_time convert_lgl_to_lwrcs_char

Documented in convert_lgl_to_lwrcs_char

#' Convert a vector of logical (TRUE/FALSE) to character ("true"/"false)
#'
#' When supplied a vector of class logical, returns a character vector
#' with the values in the vector converted to lowercase strings ("true"/"false")
#' If the vector is not logical, it is returned unchanged.
#'
#' @param col A vector of dimension 1
#'
#' @return A vector of dimension 1
#' @export
#'
#' @examples
#' convert_lgl_to_lwrcs_char(c(TRUE,TRUE,FALSE))
#' convert_lgl_to_lwrcs_char(c(1,2,3))
convert_lgl_to_lwrcs_char <- function(col) {

  if ("logical" %in% class(col)){
    col <- as.character(col)
    col <- tolower(col)
  }

  return(col)

}

#' Parse a git time/date string to an R date time object.
#'
#' @param git_dt_string A character vector of length 1 which is formatted
#' according to the time date information in a call to 'git log', e.g.
#' 'Mon May 4 11:46:11 2020 +100'.
#'
#' @return "POSIXct" time date object based on the string
#' supplied to 'git_dt_string'
#' @export
#'
#' @examples
#' parse_git_date_and_time("Mon May 4 11:46:11 2020 +0100")
#' => "2020-05-04 10:46:11 UTC"
parse_git_date_and_time <- function(git_dt_string) {
  # Note: the orders argument controls the parsing of the string
  # The following elements are expected in the following order
  # with arbitrary separators ignored:
  # a: Abbreviated weekday name in the current locale. (Also matches full name)
  # b: Abbreviated or full month name in the current locale
  # d: Day of the month as decimal number
  # H: Hours as decimal number (00–24 or 0–24)
  # M: Minute as decimal number (00–59 or 0–59).
  # S: Second as decimal number (00–61 or 0–61)
  # Y: Year with century.
  # z: ISO8601 signed offset in hours and minutes from UTC, -0800, -08:00 or -08
  # e.g.:
  # "Mon (a) May (b) 4 (d) 11 (H) : 46 (M) : 11 (S) : 2020 (Y) : +0100 (z)
  return(
    lubridate::parse_date_time(
      git_dt_string,
      orders = "abdHMSYz"
    )
  )
}
ltdroy/nlogotest documentation built on June 4, 2020, 6:17 a.m.