R/f - As date.R

Defines functions f_as_date

Documented in f_as_date

#' @title Convert string to date.
#'
#' @description Converts a string to a date.
#'
#' @author Pieter Overdevest
#'
#' @param v.input Vector with strings to be converted.
#' @param c.format Format of date in the strings (default: "%d/%m/%Y").
#' @param c.origin Starting date (default: "1899-12-30")
#'
#' @returns Vector with dates.
#'
#' @details -
#'
#' @export
#'
#' @examples
#' v.output <- f_as_date(
#'            v.input  = c("16/03/2022", "16/03/2022", "16/03/2022"),
#'            c.format = "%d/%m/%Y",
#'            c.origin = "1899-12-30"
#' )

        #################################################################################
        # FUNCTION.
        #################################################################################

        f_as_date <- function(

                v.input,

                c.format = "%d/%m/%Y",

                c.origin = "1899-12-30"
        ) {


        ######################################################################################
        # TEST
        ######################################################################################

        # v.input <- c("16/03/2022", "16/03/2022", "16/03/2022", "44635", "44634", "44634", "44635", NA, "pieter")


        ######################################################################################
        # ERROR CHECKS
        ######################################################################################


        ######################################################################################
        # INITIALIZATION
        ######################################################################################

        ######################################################################################
        # PROCESS
        ######################################################################################

        v.output <- suppressWarnings(

                case_when(

                        !is.na(as.Date(v.input,             format = "%d/%m/%Y"))    ~ as.Date(v.input,             format = "%d/%m/%Y"),
                        !is.na(as.Date(as.numeric(v.input), origin = "1899-12-30"))  ~ as.Date(as.numeric(v.input), origin = "1899-12-30"),
                        TRUE                                                         ~ NA_Date_
                )
        )


        ######################################################################################
        # ERROR CHECK
        ######################################################################################

        v.temp <- v.input[!is.na(v.input) & is.na(year(v.output))]

        if(length(v.temp) > 0) {

                warning(paste0(

                        "Note, the following 'dates' could not be converted to a valid date format: ",
                        f_paste(v.temp, b.quotation = TRUE)
                ))
        }


        ######################################################################################
        # RETURN
        ######################################################################################

        return(v.output)

        }
pieterov/generaltoolbox documentation built on Jan. 25, 2025, 10:32 a.m.