Skip to content

Add type to easily add or substract business days #53

@junder873

Description

@junder873

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions