R/yeardec2date.R

Defines functions yeardec2date

Documented in yeardec2date

#' @title Year - Date conversion
#'
#' @description Convert numeric years to dates
#'
#' @param yeardec numeric year
#'
#' @examples
#'  yeardec2date(2014.14)
#'
#' @return date in format "\%Y-\%m-\%d" (class 'Date').
#'
#' @export

yeardec2date <- function(yeardec){
  # adapted from lubridate::date_decimal
  start <- as.POSIXct(paste0(trunc(yeardec),  "/01/01"), tz="UTC")
  end   <- as.POSIXct(paste0(trunc(yeardec)+1,"/01/01"), tz="UTC")
  res <- as.Date(start + (difftime(end, start, units="secs") * (yeardec - trunc(yeardec))))
  return(res)
}

Try the TropFishR package in your browser

Any scripts or data that you put into this service are public.

TropFishR documentation built on Oct. 4, 2021, 9:06 a.m.