BusinessDays.jl icon indicating copy to clipboard operation
BusinessDays.jl copied to clipboard

Add type to easily add or substract business days

Open junder873 opened this issue 2 years ago • 0 comments

I think it would be convenient if you could do date arithmetic. For example, if I want somebody to pass a period to another function, I want to be able to have that period be a day, month, or business day. I think this could be pretty simply done by adding a new type:

struct BDay <: DatePeriod
    value::Int64
    calendar::Union{Symbol, String}
    BDay(v::Number, cal::Union{Symbol, String}) = new(v, cal)
end

Base.:(+)(dt::Date, z::BDay) = advancebdays(z.calendar, dt, z.value)
Base.:(-)(dt::Date, z::BDay) = advancebdays(z.calendar, dt, -1 * z.value)

Which allows for easy calculations:

d1 = today()
c = BDay(1, :USNYSE)
d1 - c
# 2021-09-16

And for writing easy functions:

change_days(dt::Date, c::DatePeriod) = dt + c

change_days(today(), Day(2)) # 2021-09-19
change_days(today(), BDay(2, :USNYSE)) # 2021-09-21

junder873 avatar Sep 17 '21 21:09 junder873