R/ws_date_parse.R

Defines functions ws_date_parse

Documented in ws_date_parse

#' This is a helper function to detect the date format of a character vector with dates
#'
#' @param character vector of dates
ws_date_parse <- function(date){
  # First I check if the date is a character vector and then
  # I look for a date pattern that is either hourly or daily.
  # I look for the hourly first as the daily will turn up as true
  # for hourly as well.

  if(!is.character(date)){
    stop("The date isn't a character vector.")
  }

  if(all(stringr::str_detect(date, "[0-9]{4}+[:punct:]{1}+[0-9]{2}+[:punct:]{1}+[0-9]{2}+[:blank:]{1}+[0-9]{2}:[0-9]{2}:[0-9]{2}"))){
    # Hourly
    date <- lubridate::ymd_hms(date, tz = "CET")
    return(date)
  }  
  
  else if(all(stringr::str_detect(date, "[0-9]{4}+[:punct:]{1}+[0-9]{2}+[:punct:]{1}+[0-9]{2}+[:blank:]{1}+[0-9]{2}:[0-9]{2}"))){
    # Hourly
    date <- lubridate::ymd_hm(date, tz = "CET")
    return(date)

  } else if(all(stringr::str_detect(date, "[0-9]{4}+[:punct:]{1}+[0-9]{2}+[:punct:]{1}"))){
    # Daily
    date <- lubridate::ymd(date, tz = "CET")
    return(date)

  } else {
    stop("The date format couldn't be detected.")
  }
}
krose/wattsight documentation built on May 25, 2019, 9:30 p.m.