R/validate_dates.R

#' Validate Dates
#'
#' A simple helper function that checks if dates given as arguments to others functions in
#' this package will be accepted by the host as parameters to the HTTP POST request. Especially
#' check if:
#' - The date format is dd/mm/yyyy
#' - The diference between end_date and start_date is less than or equal 3 years.
#'
#' @param start_date Character vector of length one formatted as dd/mm/yyyy
#' @param end_date Character vector of length one formatted as dd/mm/yyyy
#'
#' @return Rise an error in given invalid dates
#'
#' @examples
#' \dontrun{validate_dates('01/01/2015', '31/12/2015')}
#'

validate_dates <- function(start_date, end_date) {

    start_date_ = as.Date(start_date, '%d/%m/%Y')
    end_date_ = as.Date(end_date, '%d/%m/%Y')

    if(is.na(start_date_) | is.na(end_date_)) {
        stop('Date Should be formatted as dd/mm/yyyy (eg. 31/12/2015)')
    }

    limit_date_ = as.Date(ISOdate(as.integer(format(start_date_, '%Y')) + 3,
                                  format(start_date_, '%m'),
                                  format(start_date_, '%d')))

    if(end_date_ > limit_date_) {
        stop('Only 3 years of data can be served at once!')
    }

}
blnash508/cfm documentation built on May 30, 2019, 4:31 p.m.