Skip to content

Commit

Permalink
Merge pull request #97 from wilsonfreitas/getdate2
Browse files Browse the repository at this point in the history
Changes to optimize getdate
  • Loading branch information
wilsonfreitas authored Jun 20, 2022
2 parents 24351cb + e50d3fb commit 53c69a3
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 125 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: bizdays
Title: Business Days Calculations and Utilities
Description: Business days calculations based on a list of holidays and
nonworking weekdays. Quite useful for fixed income and derivatives pricing.
Version: 1.0.10
Version: 1.0.11
Author: Wilson Freitas <[email protected]>
Maintainer: Wilson Freitas <[email protected]>
URL: https://github.com/wilsonfreitas/R-bizdays
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# bizdays 1.0.11

* improved getdate to use a day as reference and allows expressions like:
`getdate("last bizday", Sys.Date(), "Brazil/ANBIMA")`,
`getdate("next wed", Sys.Date())`, ...(issue #28)

* organized `ref` code to avoid duplicate code

# bizdays 1.0.10

* holidaysB3 data updated, the day 2020-07-09 has been removed, it's not a holiday.
Expand Down
26 changes: 14 additions & 12 deletions R/getbizdays.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,29 @@
#' # for months
#' getbizdays("2022-12", "Brazil/ANBIMA")
#'
#' # using dates as references for months
#' dts <- seq(as.Date("2022-01-01"), as.Date("2022-12-01"), by = "months")
#' getbizdays(dts, "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$year_month)),
function(x) count_bizdays_(ref, cal, x)
seq_len(NROW(ref$ref_table)),
function(x) count_bizdays(ref, cal, x)
)
unlist(bizdays_)
}

count_bizdays_ <- function(ref, cal, ref_pos) {
ix <- if (ref$by_month) {
cal$dates.table[, "month"] == ref$year_month[ref_pos, "month"] &
cal$dates.table[, "year"] == ref$year_month[ref_pos, "year"]
} else {
cal$dates.table[, "year"] == ref$year_month[ref_pos, "year"]
}
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"])
}
Loading

0 comments on commit 53c69a3

Please sign in to comment.