R/getbizdays.R

Defines functions count_bizdays.by_year count_bizdays.by_month count_bizdays getbizdays

Documented in getbizdays

#' Obtaining business days using other dates (or month or year) as reference
#'
#' Calculates the number of business days for some specific periof of a year
#' or a month.
#' \code{getbizdays} returns the number of business days according to a
#' reference than can be another date, a month or an year.
#'
#' @param ref a reference which represents a month or year, where the date has
#' to be found.
#' @param cal the calendar's name
#'
#' \code{getbizdays} returns the number of working days according to a reference
#' that can be a month or an year.
#' This reference can be passed as a character vector representing months
#' or years, or as a numeric vector representing years. The ISO format must be
#' used to represent years or months with character vectors.
#'
#' @examples
#' # for years
#' getbizdays(2022:2024, "Brazil/ANBIMA")
#'
#' # for months
#' getbizdays("2022-12", "Brazil/ANBIMA")
#'
#' @export
getbizdays <- function(ref, cal = bizdays.options$get("default.calendar")) {
  cal <- check_calendar(cal)
  ref <- ref(ref)

  bizdays_ <- lapply(
    seq_len(NROW(ref$ref_table)),
    function(x) count_bizdays(ref, cal, x)
  )
  unlist(bizdays_)
}

count_bizdays <- function(x, ...) {
  UseMethod("count_bizdays")
}

count_bizdays.by_month <- function(ref, cal, ref_pos) {
  ix <- cal$dates.table[, "month"] == ref$ref_table[ref_pos, "month"] &
    cal$dates.table[, "year"] == ref$ref_table[ref_pos, "year"]
  sum(cal$dates.table[ix, "is_bizday"])
}

count_bizdays.by_year <- function(ref, cal, ref_pos) {
  ix <- cal$dates.table[, "year"] == ref$ref_table[ref_pos, "year"]
  sum(cal$dates.table[ix, "is_bizday"])
}

Try the bizdays package in your browser

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

bizdays documentation built on Jan. 22, 2023, 1:08 a.m.